VBA code to return text in another cell if a cell contains value

SamCha

New Member
Joined
Nov 23, 2020
Messages
30
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

Please can you help me with a code which will return text "P", "W", "N" and "GB" in columns A, B, E and F respectively. (input starts from Row 5) in Sheet 2 if column B in Sheet 1 (range starting from Row 2 - Row 1 has heading) has a value. If Column B has no value the returned field in Sheet 2 in the respective columns will be empty.

Thanks in advance.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Without some sample data and expected results (preferably with XL2BB) determining what you have and what you want is very much a guess so I'm expecting the following to be incorrect but it might get the ball rolling.

Test with a copy of your workbook.
VBA Code:
Sub Check_for_Values()
  Dim Result() As String, ResultCols() As String
  Dim i As Long, Rw As Long, col As Long, Cols As Long
  
  Result = Split("P W N GB")
  ResultCols = Split("A B E F")
  Cols = UBound(ResultCols) + 1
  With Sheets("Sheet1")
    For i = 2 To .Range("B" & Rows.Count).End(xlUp).Row
      If Len(.Range("B" & i).Value) > 0 Then
        Rw = 5 + Int((i - 2) / Cols)
        col = (i - 2) Mod Cols
        Sheets("Sheet2").Cells(Rw, ResultCols(col)).Value = Result(col)
      End If
    Next i
  End With
End Sub
 
Upvote 0
Without some sample data and expected results (preferably with XL2BB) determining what you have and what you want is very much a guess so I'm expecting the following to be incorrect but it might get the ball rolling.

Test with a copy of your workbook.
VBA Code:
Sub Check_for_Values()
  Dim Result() As String, ResultCols() As String
  Dim i As Long, Rw As Long, col As Long, Cols As Long
 
  Result = Split("P W N GB")
  ResultCols = Split("A B E F")
  Cols = UBound(ResultCols) + 1
  With Sheets("Sheet1")
    For i = 2 To .Range("B" & Rows.Count).End(xlUp).Row
      If Len(.Range("B" & i).Value) > 0 Then
        Rw = 5 + Int((i - 2) / Cols)
        col = (i - 2) Mod Cols
        Sheets("Sheet2").Cells(Rw, ResultCols(col)).Value = Result(col)
      End If
    Next i
  End With
End Sub
Hi Peter,

The code is working to some extend, however, I have missed in saying that Sheet 1 is a filtered sheet - Column B is the filtered column and only the filtered ID is copied to the Sheet 2. When I ran this code the resp columns were populated by the text P, W, N and GB but it went down to row 30 instead of just Row 5 as there is only one entry filtered for that cus no. selected in Sheet 1.
 

Attachments

  • Capture Sheet 1.PNG
    Capture Sheet 1.PNG
    7.8 KB · Views: 26
  • Capture Sheet 2.PNG
    Capture Sheet 2.PNG
    15.6 KB · Views: 26
Upvote 0
The code is working to some extend, however, I have missed in saying that Sheet 1 is a filtered sheet
Are you saying that the code could be correct provided it only looked at the visible rows?
 
Upvote 0
Actually, I don't think my previous question will resolve the problem.
As hinted at before could we have ..
some sample data and expected results (preferably with XL2BB)
(I cannot copy from a picture to test)
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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