I have had every part of this project working at some point, but for some reason Excel seems to decide every so often that it wants a different syntax for inserting a column. Initially, I used this:
targetCell.Select
MsgBox "Target Cell Selected", 0, "Got here"
Selection.EntireColumn.Insert
MsgBox "Column Inserted", 0, "Got here"
That worked great for a little while, then after working on some other parts of code, it changed its mind and for some reason decided not to like that syntax. It would select the correct cell, but wouldn't do the column insertion. In desperation, I tried a little re-ordering.
targetCell.EntireColumn.Select
MsgBox "Target Cell Selected", 0, "Got here"
Selection.Insert
MsgBox "Column Inserted", 0, "Got here"
This inexplicably worked, but I wasn't asking any questions. I kept working, and now it doesn't like either form, nor will it take
targetCell.EntireColumn.Insert
For context, let me explain the whole project. There's a sheet "instruments" that contains four columns: the vendor part number, the internal part number, the vendor name, and the row in the sheet "Sheet1" where the internal part number appears. There's a sheet "pivot" that comtains a column for each vendor and records the maximum number of vendor part numbers that correspond to one internal part number. Finally, there's a sheet "Sheet1" that has all the vendors as column headers and the internal part numbers in the leftmost column.
What I have to do is fill in Sheet1 - for every vendor-internal part number combination, put the vendor part number in the appropriate cell. In some cases there are duplicates, and in this case, we want to create an additional column for it. For instance,
----------- | VendorName
Internal P/N | ABC123, DEF456
Becomes
----------- | VendorName | VendorName2
Internal P/N | ABC123---- | DEF456
Here is all the code in its current form together:
Sub buildTable()
Dim instrumentsSheet As Worksheet
Dim sheet1Sheet As Worksheet
Dim pivotSheet As Worksheet
Dim targetCell As Range
Application.ScreenUpdating = False
Set instrumentsSheet = ThisWorkbook.Sheets("instruments")
Set sheet1Sheet = ThisWorkbook.Sheets("Sheet1")
Set pivotSheet = ThisWorkbook.Sheets("pivot")
On Error GoTo what
'Delete the existing data in all columns so that the macro can be run without
' creating extra columns accidentally
sheet1Sheet.Range("B1:IV8155").Select
Selection.Value = ""
'Replace vendor headers
Set targetCell = sheet1Sheet.Cells(1, 2)
pivotSheet.Range("B1:CX1").Copy Destination:=targetCell
'Go through and create all needed columns for every vendor (2 through 102)
For i = 2 To 102
'Parse through the maximum number of multiple entries for each vendor
Vendor = pivotSheet.Cells(1, i).Value
VendorMax = pivotSheet.Cells(2, i).Value
'Create that number of columns for the vendor
If (VendorMax > 1) Then
For j = 1 To (VendorMax - 1)
'Select the first row in that column
VendorCol = Application.Match(Vendor, sheet1Sheet.Range("A1:IV1"))
Set targetCell = sheet1Sheet.Cells(1, VendorCol + j)
'Insert the column
targetCell.EntireColumn.Insert
MsgBox "Column selected", 0, "Got here"
'Selection.Insert
MsgBox "Column inserted", 0, "Got here"
'Title the column
Set targetCell = sheet1Sheet.Cells(1, VendorCol + j)
targetCell.Value = Vendor & (j + 1)
Next j
End If
Next i
'Go through the vendor item numbers (2 to 38346)
For i = 2 To 38346
'Get the row that the internal part number is in
PartRow = instrumentsSheet.Cells(i, 4).Value
MsgBox PartRow, 0, "PartRow"
'For each vendor item number, make sure that an internal part number exists
If WorksheetFunction.IsNA(PartRow) = False Then
'Set the target cell as the leftmost cell for the vendor in the internal part
' number row
Vendor = instrumentsSheet.Cells(i, 3).Value
'MsgBox Vendor, 0, "Vendor"
VendorCol = Application.Match(Vendor, sheet1Sheet.Range("A1:IV1"))
'MsgBox VendorCol, 0, "VendorCol"
Set targetCell = sheet1Sheet.Cells(PartRow, VendorCol)
'While the target cell has something in it, move the target cell one to the right
Do While ((Len(targetCell.Value) = 0) = False)
VendorCol = VendorCol + 1
Set targetCell = sheet1Sheet.Cells(PartRow, VendorCol)
Loop
'Put the vendor part number in the target cell
targetCell.Value = instrumentsSheet.Cells(i, 1).Value
End If
If i Mod 100 = 0 Then Debug.Print i
Next i
Application.ScreenUpdating = True
what:
Debug.Print i
End Sub
I tried to comment it well enough that it would be obvious what each part is trying to do. If anyone has any ideas as to what the deuce is going on here, I would really appreciate some help. I just learned to program Excel macros yesterday, and this problem is really confusing the heck out of me.
Bookmarks