Copying a Range to a Worksheet with same Format - excel

Hi I have an issue with my code. I'm creating 4 new sheets and I'm copying a table into each one (dbR) and a range Range("B8:K8") which is a header. I'm trying to maintain the format of this range while copying, but when I run this code, the row flickers and copies nothing without showing error. Is there something I'm missing? I'm fairly new so I expect my code looks quite poor.
Sub CreateSheets()
Dim rng As Range: Set rng = ThisWorkbook.Worksheets("Configuration").Range("Vendors[Vendors]")
Dim dbR As Range: Set dbR = Sheets("Configuration").ListObjects("Client_Responses").DataBodyRange
Dim Ws_Name As String
For Each cell In rng
Ws_Name = cell
Worksheets.Add.Name = cell
ActiveSheet.Name = cell
dbR.Copy Destination:=Range("B2")
Worksheets("Configuration").Range("B8:K8").Copy
Worksheets(Ws_Name).Range("B1").PasteSpecial Paste:=xlPasteColumnWidths
Next cell
End Sub

Just add the multiple desired 'special paste's:
Sub CreateSheets()
Dim rng As Range: Set rng = ThisWorkbook.Worksheets("Configuration").Range("Vendors[Vendors]")
Dim dbR As Range: Set dbR = Sheets("Configuration").ListObjects("Client_Responses").DataBodyRange
Dim Ws_Name As String
Dim cell
For Each cell In rng
Ws_Name = cell
Worksheets.Add.Name = cell
ActiveSheet.Name = cell
dbR.Copy Destination:=Range("B2")
Worksheets("Configuration").Range("B8:K8").Copy
Worksheets(Ws_Name).Range("B1").PasteSpecial xlPasteColumnWidths
Worksheets(Ws_Name).Range("B1").PasteSpecial xlValues
Worksheets(Ws_Name).Range("B1").PasteSpecial xlFormats
Next cell
End Sub

Related

VBA set multiple variable from cell value

I want to do that; i1=A2.value i2=A3.value i3=A4.value ..... but ı dont know how to start it.
Public Sub SetVariable()
Dim cell As Range
Dim rng As Range
Dim i As Integer
Set rng = Range("B2", Range("B1").End(xlDown))
For Each cell In rng
For i = 1 To rng
i = Range("A" & cell.Row)
Next i
Next cell
End Sub

Copy and paste to variable worksheet and range

I want to copy and paste a range from a fixed worksheet ("c4:c178") into a variable worksheet.
I have two dropdowns, one has a list of all the worksheet names and the other has the column number.
My hope is the user could select the worksheet name and column reference in the drop-down and then click the macro button to copy and paste the range to that reference.
Sub CopyPaste()
Dim Sheetname As String
Sheetname = ActiveSheet.Range("i3").Value
Dim Col As Long
Col = ActiveSheet.Range("i4").Value
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Tracking Only")
Dim rng As Range
Set rng = ActiveSheet.Range("c4:C178")
With rng
ws.Cells(4, Col).Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End Sub
I receive
"Run-time error '1004': Application-define or object-defined error"
It highlights the ws.cells code.
Break your process into steps, store the dropdown values using variables, and assign the .Value of the source range to the target range.
With ThisWorkbook.Worksheets("Tracking Only")
Dim sheetName As String
sheetName = .Range("I3").Value
Dim col As Long
col = .Range("I4").Value
Dim rng As Range
Set rng = .Range("C4:C178")
End With
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(sheetName)
With rng
ws.Cells(4, col).Resize(.Rows.Count, .Columns.Count).Value = .Value
End With

Clear the cell content before the keyword

I'm trying to clear the data before the keyword TRUE including blank cells in between, and also I don't want to hardcode the range because the range will keep on change. can anyone please help me with this.
A
1
2
3
4
5
3
4
53
53
TRUE
I hope it is what you need.
Option Explicit
Sub clr()
Dim ws As Worksheet
Set ws = Sheet1 'The sheet where the data are
Dim rng As Range
Set rng = ws.Range("A:A") 'Range where the data are
Dim rngFind As Range
Set rngFind = rng.Find("TRUE") 'Find TRUE
Dim rngClear As Range
Set rngClear = Range("A1", rngFind.Offset(-1)) 'Set the range to clear
rngClear.Clear
End Sub
I assume that cell containing value "True" as the last value in the column "A". If that is the case below code will clear the content of all the cells above to it.
Sub ClearCells()
Dim lastRow As Integer
Dim actSheet As Worksheet
Set actSheet = ThisWorkbook.ActiveSheet
With actSheet
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A1:A" & lastRow - 1).Clear
End With
End Sub
Thanks,
KV Ramana

How to remove spaces at start and end of a cell?

I have an imported CSV in my spreadsheet. When I run a refresh macro it adds a space to the beginning and end of each cell.
I'm trying to remedy it by having the same macro trim all the spaces at the start and end of a cell (not in between).
Doing it on a single cell works without issue
Range("F10").Value = Trim(Range("F10").Value)
In a range it doesn't do anything, no error message, just no reaction
Dim cell As Range, areaToTrim As Range
Set areaToTrim = Sheet1.Range("A6:J10")
For Each cell In areaToTrim
cell.Value = Trim(cell.Value)
Next cell
In reality, I don't want it to be A6 to J10, I just put this in to see if it worked with a range. I want it to be A6 to J last row. So I wanted my final code to be
Dim lastrowindex As Long
lastrowindex = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Dim db_range_start As String
Dim db_range_end As String
db_range_start = "A6"
db_range_end = "J" & lastrowindex
db_range_region = db_range_start & ":" & db_range_end
Dim db_range As Range
Set db_range = Range(db_range_region)
Dim cell As Range, areaToTrim As Range
Set areaToTrim = Sheet1.Range(db_range)
For Each cell In areaToTrim
cell.Value = Trim(cell.Value)
Next cell
But this throws a
Method Value of object Range failed.
How about the following, just took your code and simplified it, as you declared db_range as a Range and then tried to use it to specify the range using Sheet1.Range(db_range) which would take a string instead of a Range:
Sub Trim_Range()
Dim ws As Worksheet: Set ws = Sheet1
Dim LastRow As Long
Dim c As Range
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For Each c In ws.Range("A6:J" & LastRow)
c.Value = Trim(c.Value)
Next c
End Sub

Excel VBA - Dynamic Range to use with For Each Statement

I'm trying to dynamically define a range in row like ctrl+down or ctrl+shift+down (to next blank cell) to be used with "For Each itm In rng" statement.
Originally I had it static like this set rng = Range("A4:A10")
So I tried to change it to something like this
Dim rng As Range
Set rng = Range("A4").CurrentRegion.Rows.Count
For Each itm In rng
...
Next itm
I also tried something like this
Set StartCell = Range("A4")
rng = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
But the code doesn't seems to work with "For Each itm In rng" statement
Any help is very much appreciated.
You can use .xlDown, it's the equivalent of pressing ctrl+Shift+down.
Dim rng As Range
Dim lastRow As Long
lastRow = Range("A4").End(xlDown).Row
Set rng = Range("A4:A" & lastRow)
For Each itm In rng
'do something
Next itm
Try this if it helps:
Option Explicit
Sub Test()
Dim LastRow As Long 'to find the last row on your range
Dim MyRange As Range 'to reference the whole range
Dim C As Range 'to loop through your range
With ThisWorkbook.Sheets("MySheet") 'change MySheet for your sheet name
LastRow = .Cells(4, 1).End(xlDown).Row 'last row, how? You go down to the last row and then ctrl + up
Set MyRange = .Range("A4:A" & LastRow) 'dynamic range
For Each C In MyRange
'your code
Next C
End With
End Sub

Resources