Hi all,
I don't use VBA very often, but I'm hoping someone can help me with this issue I'm having. The code is posted below, my issue is with the sz_col variable in the Columns.Select command
I put the MsgBoxes in to check that the variable was being saved correctly, and it is. If I replace sz_col with 6 then the code works fine. The MsgBox tells me that sz_col = 6. But if I put the sz_col in the command I get an Application-defined or object-defined error. I don't want to use a hard-coded cell reference because I can't control the source document.
I actually have the same problem in another piece of code where it is copying and pasting from 6 different columns on sheet A to sheet B. Columns 1 - 5 copy fine, but in the 6th I get the same error from the variable. Again, if I replace it with the column reference it works fine.
and I've used MsgBoxes to check that the reference is being stored correctly. I put that below as well, just in case....
Thanks for any help you can give!
This works:
This doesn't:
I don't use VBA very often, but I'm hoping someone can help me with this issue I'm having. The code is posted below, my issue is with the sz_col variable in the Columns.Select command
I put the MsgBoxes in to check that the variable was being saved correctly, and it is. If I replace sz_col with 6 then the code works fine. The MsgBox tells me that sz_col = 6. But if I put the sz_col in the command I get an Application-defined or object-defined error. I don't want to use a hard-coded cell reference because I can't control the source document.
I actually have the same problem in another piece of code where it is copying and pasting from 6 different columns on sheet A to sheet B. Columns 1 - 5 copy fine, but in the 6th I get the same error from the variable. Again, if I replace it with the column reference it works fine.
and I've used MsgBoxes to check that the reference is being stored correctly. I put that below as well, just in case....
Thanks for any help you can give!
Code:
Sub UPC_Concat()
Dim LastRowU As Long
Dim sty, sz As Range
Dim sty_row, sz_col As String
LastRowU = Sheets("UPCs").Cells(Rows.Count, 2).End(xlUp).Row
Set sty = Sheets("UPCs").Range("A1:P" & LastRowU).Find("STYLE #", LookAt:=xlWhole) 'Find style # on the UPC list to set top row
sty_row = Sheets("UPCs").Range(sty.Address).Row
Set sz = Sheets("UPCs").Range("A1:P" & LastRowU).Find("SIZE", LookAt:=xlWhole) 'Find size on the UPC list to set where replace needs to occur
MsgBox sz.Address
sz_col = Sheets("UPCs").Range(sz.Address).Column
MsgBox sz_col
Columns(sz_col).Select
Selection.Replace What:="-", Replacement:=".5", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
For i = sty_row + 1 To LastRowU
Worksheets("UPCs").Range("A" & i).Value = Worksheets("UPCs").Range("B" & i).Value & "-" & _
Worksheets("UPCs").Range("D" & i).Value & "-" & Worksheets("UPCs").Range("F" & i).Value
Next i
Columns(sz_col).Select
Selection.Replace What:=".5", Replacement:="-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub
Code:
Set st = Sheets("PO").Range("A1:P" & LastRow).Find("STYLE #", LookAt:=xlWhole) 'Find style # on the PO then copy & paste into data sheet
Set st2 = Sheets("PO").Range(st.Address).Offset(1, 0)
st_col = Sheets("PO").Range(st.Address).Column
Worksheets("PO").Range(st2.Address, Cells(LastRow - 1, st_col)).Copy Worksheets("Data").Range("D" & LastRow2 + 1)
Set sz = Sheets("PO").Range("A1:P" & LastRow).Find("SIZE", LookAt:=xlWhole) 'Find size on the PO then copy & paste into data sheet
Set sz2 = Sheets("PO").Range(sz.Address).Offset(1, 0)
sz_col = Sheets("PO").Range(sz.Address).Column
Worksheets("PO").Range(sz2.Address, Cells(LastRow - 1, sz_col)).Copy Worksheets("Data").Range("A" & LastRow2 + 1)
Set col = Sheets("PO").Range("A1:P" & LastRow).Find("COLOR", LookAt:=xlWhole) 'Find color on the PO then copy & paste into data sheet
Set col2 = Sheets("PO").Range(col.Address).Offset(1, 0)
col_col = Sheets("PO").Range(col.Address).Column
Worksheets("PO").Range(col2.Address, Cells(LastRow - 1, col_col)).Copy Worksheets("Data").Range("F" & LastRow2 + 1)
Set d = Sheets("PO").Range("A1:P" & LastRow).Find("DESCRIPTION", LookAt:=xlWhole) 'Find description on the PO then copy & paste into data sheet
Set d2 = Sheets("PO").Range(d.Address).Offset(1, 0)
d_col = Sheets("PO").Range(d.Address).Column
Worksheets("PO").Range(d2.Address, Cells(LastRow - 1, d_col)).Copy Worksheets("Data").Range("E" & LastRow2 + 1)
Set cst = Sheets("PO").Range("A1:P" & LastRow).Find("ORDER AMOUNT", LookAt:=xlWhole) 'Find cost on the PO then copy & paste into data sheet
Set cst2 = Sheets("PO").Range(cst.Address).Offset(1, 0)
cst_col = Sheets("PO").Range(cst.Address).Column
Worksheets("PO").Range(cst2.Address, Cells(LastRow - 1, cst_col)).Copy Worksheets("Data").Range("G" & LastRow2 + 1)
Set qty = Sheets("PO").Range("A1:P" & LastRow).Find("TOTAL ORDER QTY", LookAt:=xlWhole) 'Find quantity on the PO then copy & paste into data sheet
Set qty2 = Sheets("PO").Range(qty.Address).Offset(1, 0)
qty_cl = Sheets("PO").Range(qty.Address).Column
Worksheets("PO").Range(qty2.Address, Cells(LastRow - 1, qty_cl)).Copy Worksheets("Data").Range("C" & LastRow2 + 1)
This works:
VBA Code:
Worksheets("PO").Range(qty2.Address, Cells(LastRow - 1, 8)).Copy Worksheets("Data").Range("C" & LastRow2 + 1)
This doesn't:
VBA Code:
Worksheets("PO").Range(qty2.Address, Cells(LastRow - 1, qty_cl)).Copy Worksheets("Data").Range("C" & LastRow2 + 1)