Hi, all --
I'm not having much luck searching the forum -- I think because I may not be using the word "variable" correctly.
I'm hoping someone will get past my (mis)?use of terminology and grasp what I'm shooting for.
I've got a macro that uses a lot of cell.Offset(x,y).Value statements.
Without going into too much excruciating detail, it basically loops through a column, looks at the data in the same row in a different column, and then inserts that data into yet another column.
Here's the code in question:
Sub FindUPCs()
Dim UPC As Range
Dim UPC1 As Range
Set UPC = Range("A1:Z1").Find("UPC")
Set UPC1 = UPC.Offset(1)
Dim UPCCol As Range
Set UPCCol = Range(UPC1, UPC1.End(xlDown))
Dim cell As Range
Dim MyNumber As Long
MyNumber = 1
For Each cell In UPCCol
If cell.Value = cell.Offset(1) And Not cell.Offset(0, 2).Value = cell.Offset(1, 2).Value Then
cell.Offset(0, 10).Value = cell.Offset(0, 2).Value & " • " & cell.Offset(1, 2).Value
cell.Offset(1, 10).Value = cell.Offset(0, 2).Value & " • " & cell.Offset(1, 2).Value
Else
cell.Offset(1, 10).Value = cell.Offset(0, 2).Value
End If
Next
End Sub
I'm going to have to do 5 or 6 more columns worth of this.
It's going to be a little time-consuming to type out each and every cell.Offset(x,y).value statement, and (for me anyway) it's going to get a little confusing to keep track of the various Offsets so that the right cells are being read and written to.
What I'd like to do is assign a variable "name" to each one. For example, cell.Offset(0,2).value refers to cells in a column titled "studio."
The idea I had was to define a variable that would represent that snippet of code.
Thus, instead of this section:
For Each cell In UPCCol
If cell.Value = cell.Offset(1) And Not cell.Offset(0, 2).Value = cell.Offset(1, 2).Value Then
cell.Offset(0, 10).Value = cell.Offset(0, 2).Value & " • " & cell.Offset(1, 2).Value
cell.Offset(1, 10).Value = cell.Offset(0, 2).Value & " • " & cell.Offset(1, 2).Value
Else
cell.Offset(1, 10).Value = cell.Offset(0, 2).Value
I could have something that looked like this:
For Each cell In UPCCol
If cell.Value = cell.Offset(1) And Not StudioA = StudioB Then
cell.Offset(0, 10).Value = cell.Offset(0, 2).Value & " • " & cell.Offset(1, 2).Value
cell.Offset(1, 10).Value = cell.Offset(0, 2).Value & " • " & cell.Offset(1, 2).Value
Else
cell.Offset(1, 10).Value = cell.Offset(0, 2).Value
To that end, I've tried a number of variations on:
Dim StudioA As String
StudioA = "cell.Offset(0, 2).Value"
Dim StudioB As String
StudioB = "cell.Offset(1, 2).Value"
For Each cell In UPCCol
If cell.Value = cell.Offset(1) And Not StudioA = StudioB Then
cell.Offset(0, 10).Value = cell.Offset(0, 2).Value & " • " & cell.Offset(1, 2).Value
cell.Offset(1, 10).Value = cell.Offset(0, 2).Value & " • " & cell.Offset(1, 2).Value
Else
cell.Offset(1, 10).Value = cell.Offset(0, 2).Value
End If
Next
But it doesn't work; the code just ignores the "StudioA" and "StudioB" section.
Obviously defining them as Strings isn't working.
I tried defining them as Ranges and using this:
If cell.Value = cell.Offset(1) And Not StudioA.Value = StudioB.Value Then
but that just errors out.
Can this be done and I'm just missing something, or am I attempting something that Excel just doesn't do?
Fingers crossed that it's me and not Excel
Bookmarks