Q: How to combine a Cell with a counter value in VBA

peter_z

Board Regular
Joined
Feb 27, 2011
Messages
87
Hey Guys, can anyone see what is wrong with the following code:

Code:
               With Worksheets(strSource)
              .Visible = True
 
              lngCounterC = Worksheets("REFERENCE").Range("L:L").Cells.SpecialCells(xlCellTypeConstants).Count
              lngCounterC = lngCounterC - 1
 
              Sheets("Reference").Select
              Range("S8").Copy
              Range("'P7:P' & lngCounterc & ").Select
              Selection.Paste
 
              Worksheets(strSource).Visible = xlSheetVeryHidden
              End With

Error: Method 'Range' of object' _Global failed
Run-time error '1004'

Many thanks in advance
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello,

maybe change:

Range("'P7:P' & lngCounterc & ").Select

to:

Range("P7:P & lngCounterc & ").Select
 
Upvote 0
Yup thats the line that gets highlighted, no luck with that formula either... also just realised that this code wouldn't paste into all the cells anyway :S

Do you know any code that would paste it into every individual cell selected?
 
Upvote 0
Trying the following which is allowing me to go through the whole formula... it just isn't displaying any change :S

Code:
              With Worksheets(strSource)
              .Visible = True
                
              lngCounterC = Worksheets("REFERENCE").Range("L:L").Cells.SpecialCells(xlCellTypeConstants).Count
              lngCounterC = (lngCounterC - 1)
                
              For i = 1 To lngCounterC
                
              Sheets("Reference").Select
              Range("S8").Copy
              Range("P7:P" & i).Select
              activeSelection.Paste
              
              Next i
              
              Worksheets(strSource).Visible = xlSheetVeryHidden
              End With
 
Last edited:
Upvote 0
What is lngCounterc dimmed as?

You may find a hint as to what happening with this line (place before the line in question)

Code:
msgbox lngCounterc

this will show what value the variable has.

this code works for selecting
Code:
Range("P7:P" & lngCounterc).Select

The only way I can think of is a for each loop to place the "paste" into each cell.
If we can get the first issue resolved, maybe we can also get the paste.
 
Upvote 0
<font face=Courier New><SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cell <SPAN style="color:#00007F">In</SPAN> Range("P7:P" & lngCounterc)<br>    cell.Select<br>    Range("S8").Copy<br>    ActiveCell.Paste<br><SPAN style="color:#00007F">Next</SPAN> cell<br></FONT>
 
Upvote 0
The above code takes us back to the error:
Run-time error '1004'
Method 'Range' of object' _Global' failed

Selecting the line:
For Each cell In Range("P7:P" & lngCounterC)
 
Upvote 0
Here is something that may work. This will only mirror the value of the cell S8, in other words, no formats.


<font face=Courier New><SPAN style="color:#00007F">With</SPAN> Worksheets(strSource)<br>    .Visible = <SPAN style="color:#00007F">True</SPAN><br>      <br>    lngCounterc = Worksheets("REFERENCE").Range("L:L").Cells.SpecialCells(xlCellTypeConstants).Count<br>    lngCounterc = (lngCounterc - 1)<br>      <br>      <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cell <SPAN style="color:#00007F">In</SPAN> Range("P7:P" & lngCounterc)<br>          cell.Value = Range("S8").Value<br>      <SPAN style="color:#00007F">Next</SPAN> cell<br>    <br>    Worksheets(strSource).Visible = xlSheetVeryHidden<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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