VBA amend code to add text!

VBA learner ITG

Active Member
Joined
Apr 18, 2017
Messages
272
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Good afternoon,

The below code only adds the worksheet name to column cell range D2-D5 regardless of what text is in cells D2-D5.

Can I get your guidance to amend the code that if there is a word "Category" or "Store" in column D that it adds the worksheet Name and _with the cell value which is either going to be "Category" "Store" instead of it only being applicable to cells D2-D5 per the below code?

e.g:

Sheet1_Category
Sheet1_Store

Code:
 ws.Range("D2").Value = ws.Name & "_" & ws.Range("D2").Value
                ws.Range("D3").Value = ws.Name & "_" & ws.Range("D3").Value
                ws.Range("D4").Value = ws.Name & "_" & ws.Range("D4").Value
                ws.Range("D5").Value = ws.Name & "_" & ws.Range("D5").Value
                DoEvents
                ws.Copy
                ActiveWorkbook.SaveAs sheetDirectory & Application.PathSeparator & ws.Name & "_" & wbName
                DoEvents
                ActiveWorkbook.Close
                validWs = True
        End If
    Next ws
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I am not quite clear what your question/issue is.

Are you saying that that there might be more cells in column D other than D2:D5 that you want to apply this to?
And are you saying that you only want to apply this to cells that have the words "Category" or "Store" in them?
Does this mean that you just want to ignore the other cells that have other values in them?
 
Upvote 0
Good afternoon,

found a solution by amending the code to the below:

categoryData = ws.Range("D1").Resize(ws.UsedRange.Rows.count, 1)
For i = 1 To UBound(categoryData)
If categoryData(i, 1) = "store" Then categoryData(i, 1) = ws.Name & "_" & categoryData(i, 1)
If categoryData(i, 1) = "category" Then categoryData(i, 1) = ws.Name & "_" & categoryData(i, 1)
Next i
ws.Range("D1").Resize(ws.UsedRange.Rows.count, 1) = categoryData
DoEvents
ws.Copy
ActiveWorkbook.SaveAs sheetDirectory & Application.PathSeparator & ws.Name & "_" & wbName
DoEvents
ActiveWorkbook.Close
validWs = True
End If
Next ws
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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