I'm relatively new to Excel VBA, and I'm not sure what code I need

ctouchberry

New Member
Joined
Dec 4, 2017
Messages
40
I'm relatively new to VBA, and have run into a "not knowing what code I need" section. I might have used the wrong terminology in my explanation, I apologize in advance.

I'm loading Room and Floor data from Component to JCX (with a cross-sheet check on the Floor data from Space). The problem is, for some instances in the source data on
Component, the Rooms are listed as "Room 1, Room 2" (in one cell). That same data is listed in Space as "Room 1", "Room 2" (in cells on top/below each other).

If I had my way on
Component, it would be Room 1 in one cell, and then Room 2 in the cell below it. But I can't change the source data.

I need to make my "Room" logic in the first phase account for a "Value1, Value2" situation, but not error if there's only one value. In this updated logic, I need each "Value2" to be in the cell below "Value1", and then copy the associated Tag Number (on the JCX screenshot, that would be the values in the 3rd column.

I also need to apply that same logic to the second phase, as the VLookup right now is looking at the "Value1, Value2" cell in
Component, comparing it with a "Value" cell in Space, erroring, and putting a #N/A on the Floor cell in JCX associated with those specific Rooms.

Component
https://imgur.com/a/rI2Pi
Space
https://imgur.com/a/LC5zM
JCX
https://imgur.com/a/ktojj

Code:
'First Phase[COLOR=#242729][FONT=Arial]
[/FONT][/COLOR]'Tag Number and Room
'Row Column
Do
    'Tag Number
    Sheets("JCX").Cells(TagRowCounter, 10).Value = Sheets("Component").Cells(TagRowCounter, 1).Value
    'Room
    Sheets("JCX").Cells(TagRowCounter, 9).Value = Sheets("Component").Cells(TagRowCounter, 5).Value
    TagRowCounter = TagRowCounter + 1
        
Loop Until Sheets("Component").Cells(TagRowCounter, 1) = False
Sheets("JCX").Columns("A:AM").AutoFit

'Second Phase
'Floor
'Row Column
TagRowCounter = 2
Do
    Sheets("JCX").Cells(TagRowCounter, 8).Value = Application.VLookup(Sheets("Component").Cells(TagRowCounter, 5), Sheets("Space").Range("A:E"), 5, 0)
    TagRowCounter = TagRowCounter + 1
    
Loop Until Sheets("Component").Cells(TagRowCounter, 5) = False
Sheets("JCX").Columns("A:AM").AutoFit
 
Last edited:
Right now, TagRowCounter is 318 and i is 59.

My step results are If Not line, Else line, i = line, End If line, Loop Until line, repeat.
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
That's fine.
What happens when you get to another value that does not contain a coma?
 
Upvote 0
Add this
Code:
    Sheets("JCX").Range("A:CZ").NumberFormat = "@"
    i = 0
Do
    If Not InStr(Sheets("Component").Cells(tagrowcounter + i, 5).Value, ",") > 0 Then
        Sheets("JCX").Cells(tagrowcounter, 10).Value = Sheets("Component").Cells(tagrowcounter + i, 1).Value
        'Room
        Sheets("JCX").Cells(tagrowcounter, 9).Value = Sheets("Component").Cells(tagrowcounter + i, 5).Value
        tagrowcounter = tagrowcounter + 1
    Else
      [COLOR=#ff0000]MsgBox Sheets("Component").Cells(tagrowcounter + i, 5).Value & vbLf & "Row " & tagrowcounter + i[/COLOR]
        i = i + 1
    End If
Loop Until Sheets("Component").Cells(tagrowcounter + i, 1) = False
End Sub
Every time you encounter a cell with a , a msgbox will appear showing you the cell value & the row number. Does the message change?
 
Upvote 0
The box comes up on Row 318, with "1B11, 1B04" as values. And again on 319 with "1A03, 1AC5".

This is consistent with the source content on Component. I assume this will happen until there's a cell with no comma.
 
Upvote 0
I assume this will happen until there's a cell with no comma.
That's right, but it shows that the code is doing what it's meant to.
Obviously having the msgbox come up all the time is going to get annoying, so you can simply delete the line in red.
 
Upvote 0
The code may be doing what it's supposed to do, but that doesn't mean it's doing what I need it to do.

I just need to skip the comma values entirely. If a value order went like this in Component: Normal, Normal, Normal, Comma, Comma, Normal, Normal...then I just need it to be Normal, Normal, Normal, Normal, Normal. With no placeholder rows for those Comma values. Sorta like the "hide" function in Excel.
 
Upvote 0
Does it copy all the "normal" values to the JCX sheet?
 
Upvote 0
Without being able to see your data, I'm not sure that I can help any further. With data like this

Excel 2013 32 bit
ABCDE
1A12345
2A2a
3A3b
4A4c
5A5d
6A6e,e
7A7f,f
8A8g
9A9h
10A10i,i
11A11j, j
12A12k
Component


I get


Excel 2013 32 bit
HIJ
1
2aA2
3bA3
4cA4
5dA5
6gA8
7hA9
8kA12
JCX
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top