Selecting a specific range based on a stored value

_Echo_2

New Member
Joined
Apr 21, 2016
Messages
13
Hello,

I'm new to VBA and have been developing a macro for a database. I've completed about 95% of it and it all works successfully.

However, I'm having trouble with the last thing I want it to do, which is replacing data.

So here is the situation:

I have a form and a 'master' sheet. The master sheet is a collection of certain information from all the different forms (all forms have their own individual sheets in the workbook). The master is sorted alphanumerically by the data in column H. Data is on the master sheet from row 11 downwards (rows are being added and sorted every time a form is made) and from columns B to AC.

Lets say on the master sheet there are 3 rows in column H that have the same value. I want the macro to search column H for all instances of that value (it should find 3 cells). I want it to then select the range as follows: the row and column B of the first time the value appears (B14) to the row and column AC of the last time the value appears (AC16). So it looks like this:

AB...H...ABAC
11M1V0
12M2V1
13M3V1
14M4dataV2datadatadata
15M5dataV2datadatadata
16M6dataV2datadatadata
17M7V3
...
x

<tbody>
</tbody>
So referring to the table above, I want the macro to find all "V2" (V2 would be a stored value) in column H then select the range B14:AC16.

Below is the code that I have that precedes the step I'm asking about above. The Do Until Empty loop will create a block of data on the sheet "Temp." The block of data is the same size as the range I want to select on the master sheet (i.e. 3 rows, 28 columns). I want to copy the block of data on "Temp" and then paste it into the selected range on the master (i.e. 'Pasting over' or replacing the data on the master). strName is the stored value (V2 in the example table).
_____________________________
Sub Replace_Test()

Dim strName As String
strName = ActiveSheet.Range("C2")​
Dim ws As Worksheet

With ThisWorkbook
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = "Temp"​
End With

Sheets(strName).Select

Ct = 20

Do
Range("C" & Ct, "F" & Ct).Select
Selection.Copy
Range("P2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True​
Application.CutCopyMode = False
Range("P2:P29").Copy
Sheets("Temp").Activate
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True​
Application.CutCopyMode = False
Sheets(strName).Select
Ct = Ct + 1
Loop Until IsEmpty(Cells(Ct, 3))

Sheets("Temp").Activate
Range(Range("A2"), Range("AB2").End(xlDown).End(xlToRight)).Select
Selection.Copy
Sheets("Master").Activate

??? Search col H for strName
??? Select range as intended

Paste

End Sub
_____________________________



If anything is unclear or I could explain things better please let me know. As I said, I'm learning VBA and this is my first post on the forum.

Thank you!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,216,069
Messages
6,128,599
Members
449,460
Latest member
jgharbawi

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