I am looking for a way to select an entire row but skip the first 3 columns of the same row without using 'range()' command. What command can i use?
You can use a combination of Cells and Resize:
Range.Cells Property
Range.Resize Property
Depending on how you ask the question (skip first column or first column is), you can use the combination as follows:
Option Explicit
Sub EntireSkipColumns()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim rng As Range
Dim FR As Long: FR = 2
Dim LR As Long: LR = 10
Dim i As Long
Dim j As Long: j = 3 ' Skip first 3 columns
For i = FR To LR
Set rng = ws.Cells(i, j + 1).Resize(, ws.Columns.Count - j)
With rng
' To check if the range is correct.
Debug.Print .Address(False, False)
' Cycle Interior ColorIndex
'.Interior.ColorIndex = i
End With
Next i
End Sub
Sub EntireFirstColumn()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim rng As Range
Dim FR As Long: FR = 2
Dim LR As Long: LR = 10
Dim i As Long
Dim j As Long: j = 4 ' Use 4 as the first column
For i = FR To LR
Set rng = ws.Cells(i, j).Resize(, ws.Columns.Count - j + 1)
With rng
' To check if the range is correct.
Debug.Print .Address(False, False)
' Cycle Interior ColorIndex
'.Interior.ColorIndex = i
End With
Next i
End Sub
EDIT:
Set rngTarget = rngTarget.Offset(1) is only used to move each result a row below.
Sub QualifyCellsToo()
Dim wsSource As Worksheet: Set wsSource = ThisWorkbook.Worksheets("Sheet1")
Dim wsTarget As Worksheet: Set wsTarget = ThisWorkbook.Worksheets("Sheet2")
Dim rngSource As Range
Dim rngTarget As Range
' This is wrong:
'Worksheets("sheets1").Range(Cells(3, 4), Cells(3, 9)).Copy _
Worksheets("sheets2").Range(Cells(3, 4), Cells(3, 9))
' You have to qualify 'Cells', too:
Worksheets("Sheet1").Range(Worksheets("Sheet1").Cells(3, 4), _
Worksheets("Sheet1").Cells(3, 9)).Copy _
Worksheets("Sheet2").Range(Worksheets("Sheet2").Cells(3, 4), _
Worksheets("Sheet2").Cells(3, 9))
' This is a long expression, so using variables is preferred.
Set rngSource = wsSource.Range(wsSource.Cells(3, 4), wsSource.Cells(3, 9))
Set rngTarget = wsTarget.Range(wsTarget.Cells(3, 4), wsTarget.Cells(3, 9))
Set rngTarget = rngTarget.Offset(1)
rngTarget.Resize(10).Clear
' Copy values or formulas and formats using same sized ranges.
rngSource.Copy rngTarget
Set rngTarget = rngTarget.Offset(1)
' Copy values or formulas and formats using only the first cell
' of Target Range.
rngSource.Copy rngTarget.Cells(1)
Set rngTarget = rngTarget.Offset(1)
' Copy values
rngTarget.Value = rngSource.Value
Set rngTarget = rngTarget.Offset(1)
' Copy values using target without '.Value'
rngTarget = rngSource.Value
Set rngTarget = rngTarget.Offset(1)
End Sub
Related
I'm trying to single out a single cell, not a constant as it depends on the data at hand.
Dim lastrow As Integer
Dim sumcount As Integer
'find last non-empty row in column C
lastrow = Cells(Rows.Count, 3).End(xlUp).Row
'add up the int values in the range in C
sumcount = Application.WorksheetFunction.Sum(Range("C17", Cells(lastrow, 3)))
'result goes into the next empty cell in C
Cells(lastrow + 1, 3).Value = sumcount
Now I'm trying to throw the cell Cells(lastrow + 1, 3) into a variable so I can offset it and assign that offset cell a string value.
As far as I know that's only possible with the Range object, but I can't figure out the syntax to achieve that. Is there a property or method I'm not aware of?
Add Column Total and Write to Cell Adjacent To the Right
Option Explicit
Sub Test()
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
Dim sfCell As Range: Set sfCell = ws.Range("C17")
Dim slCell As Range: Set slCell = ws.Cells(ws.Rows.Count, "C").End(xlUp)
Dim srg As Range: Set srg = ws.Range(sfCell, slCell)
Dim sSum As Double: sSum = Application.Sum(srg)
Dim dCell As Range: Set dCell = slCell.Offset(1)
dCell.Value = sSum
dCell.Offset(, 1) = "some string" ' adjacent to the right
End Sub
Cells returns a range object. So you should be able to simply do:
set myrange = cells(lastrow + 1, 3)
myrange.value = "string"
I have a list of row numbers that I need to keep. All other rows need deleted.
This macro deletes entire rows based on row numbers in a list. It works exactly as intended.
How can it be altered to delete all rows EXCEPT those rows on the list?
Dim deleteRows As Range
Dim data() As Variant
Dim i As Double
Dim SourceWks As Worksheet
Dim oldWks As Worksheet
Set SourceWks = Sheets("TBDws")
Set oldWks = Sheets("TBDsamples")
With SourceWks
data = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
End With
Set deleteRows = oldWks.Rows(data(1, 1))
For i = 2 To UBound(data, 1)
Set deleteRows = Union(deleteRows, oldWks.Rows(data(i, 1)))
Next i
deleteRows.Delete Shift:=xlUp
End Sub
This will delete all the rows on the sheet TBDsamples that aren't listed in column A on TBDws
Sub DeleteThings()
Dim SourceWks As Worksheet
Dim oldWks As Worksheet
Dim deleteRange As Range
Dim arrRows() As Variant
Dim Res As Variant
Dim I As Long
Set SourceWks = Sheets("TBDws")
Set oldWks = Sheets("TBDsamples")
With SourceWks
arrRows = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
End With
For I = 1 To oldWks.Range("A" & Rows.Count).End(xlUp).Row
Res = Application.Match(I, arrRows, 0)
If IsError(Res) Then
If deleteRange Is Nothing Then
Set deleteRange = oldWks.Rows(I)
Else
Set deleteRange = Union(deleteRange, oldWks.Rows(I))
End If
End If
Next I
deleteRange.Delete Shift:=xlUp
End Sub
I'm trying to find the last empty row on column "D" and then perform a Vlookup function from matching value from column "A" with another sheet, and autofill this formula down.
The problem is that i'm getting an error in the vlookup.
Dim myRange As Range
Set myRange = Worksheets("IDL MASTER").Range("C:AV")
Dim Rng As Range
Set Rng = Worksheets("Planilha2").Range("A:A")
LastBlankRow = Cells(Rows.Count, 4).End(xlUp).Row + 1
Cells(LastBlankRow, 4).Select
ActiveCell.Value = Application.WorksheetFunction.VLookup(Rng, myRange, 46, False)
ActiveCell.AutoFill Destination:=Range(ActiveCell, Cells(Cells(Rows.Count, "A").End(xlUp).Row, "D"))
End sub
Sub FillColumnD()
Dim ws1 As Worksheet: Set ws1 = Worksheets("IDL MASTER")
Dim ws2 As Worksheet: Set ws2 = Worksheets("Planilha2")
Dim lRow_ColA As Integer: lRow_ColA = ws2.Cells(Rows.Count, 1).End(xlUp).Row
Dim lRow_ColD As Integer: lRow_ColD = ws2.Cells(Rows.Count, 4).End(xlUp).Row + 1
Dim LookupRange As Range: Set LookupRange = ws1.Range("C:AV")
Dim FillRange As Range: Set FillRange = ws2.Range(Cells(lRow_ColD, 4), Cells(lRow_ColA, 4))
Dim rCell As Range
For Each rCell In FillRange
rCell.FormulaR1C1 = Application.WorksheetFunction.VLookup(rCell.Offset(0, -3), LookupRange, 46, False)
Next rCell
End Sub
What change do i need to do in the below code so that the entire row is copied into a defined number of rows and not just the first column?
Sub InsertSessions()
Dim Rng As Long
Dim k As Long
Dim rRange As Range
Set rRange = Selection
ActiveCell.EntireRow.Select
Rng = InputBox("Enter number of sessions:.")
For k = 1 To Rng
Rows(rRange.Row).Insert Shift:=xlDown, _
CopyOrigin:=xlFormatFromLeftOrAbove
Call rRange.Copy(Range(Cells(rRange.Row - 1, rRange.Column), Cells(rRange.Row - 1, rRange.Column)))
Next k
End Sub
this should work with no loop needed:
Option Explicit
Sub InsertSessions()
Dim rRange As Range
Set rRange = Selection.EntireRow
Dim Rng As Long
Rng = InputBox("Enter number of sessions:.") * rRange.Rows.Count
With ActiveSheet
Dim StartRng As Range
Set StartRng = .Cells(rRange.Cells(rRange.Rows.Count, 1).Offset(1), 1)
StartRng.Resize(Rng).Insert xlDown
rRange.Copy .Range(.Cells(StartRng, 1), .Cells(StartRng.Offset(Rng - 1), 1))
End With
End Sub
I want to convert a range of cells from integer to String. However, since I have so much data, I can't use a standard loop for ranges as it takes too long.
Instead I thought to use the array and convert the desired range(array) into string values.
This is what I tried to do by modifying my standardcode that converts range into string just instead range I would use in the below the array:
Sub CovertToString()
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
Dim sArray As Variant
Dim LastRow As Integer
Dim cell As Variant
With ws
LastRow = .Cells(.rows.Count, 1).End(xlUp).row
sArray = .Range(.Cells(1, 8), .Cells(LastRow, 8))
For Each cell In sArray
cell = "'" & cell.Value
Next
End With
End Sub
Unfortunately, It does not work which I understand as I am not sure how to correct it.
This way will convert the cell formats to Text:
Sub ConvertToString()
Dim ws As Worksheet
Dim LastCell As Range
Dim rCell As Range
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ws
Set LastCell = .Cells(.Rows.Count, 1).End(xlUp).Offset(, 7)
'Convert format to 'Text'
.Range(.Cells(1, 8), LastCell).NumberFormat = "#"
End With
End Sub
This way will copy the range to an array and add a ' to each value before posting back to the sheet:
Sub ConvertToString()
Dim ws As Worksheet
Dim LastCell As Range
Dim vValues() As Variant
Dim R As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ws
'Your code is looking for last cell in column A, so offset to column H once found.
'This is a reference to the last cell, not the row number so can be used in the range.
Set LastCell = .Cells(.Rows.Count, 1).End(xlUp).Offset(, 7)
vValues = .Range(.Cells(1, 8), LastCell).Value
'Add a ' to each value.
For R = 1 To UBound(vValues, 1)
vValues(R, 1) = "'" & vValues(R, 1)
Next R
'Paste back to sheet.
.Range(.Cells(1, 8), LastCell) = vValues
End With
End Sub
Further reading on arrays & worksheets