VBA How to delete in a specific direcitons? - vba

I want to delete all values starting from A2:O2 and LOWER , i.e. A3:O3, A4:O4, A5:O5
A B C .... O P Q
1 ..............................
2 deletedeletedeletedeletedelete
3 deletedeletedeletedeletedelete
4 deletedeletedeletedeletedelete
5 deletedeletedeletedeletedelete
I am not sure how to do this
Sub DeleteLower()
Range("A2:O1048576").Select
Selection.SpecialCells(xlCellTypeConstants, 23).Select
Selection.ClearContents
end sub
How do I explain that I need the range of "A2:0 infinity " "A2:O1048576" ?

Perhaps something like this would work for you:
Sub RemoveValues()
ActiveSheet.Range(ActiveSheet.Range("A2:O2").Address, ActiveSheet.Cells(Rows.Count, 1).Address).ClearContents
End Sub
This will remove all content from cells A2:O2 and downward.

Related

VBA numbering loop

I am trying to simplify a code in a Macro for numbering rows of specific columns of excel. Currently I am using
With Sheet1.Range("V6")
.Value = 1
.AutoFill .Resize(V6 + C, 1), xlFillSeries
End With
The macro has already set "C" as a variable that can change each time it is run. I want to simplify the code because I don't know how to loop this to repeat in every third column. I have tried For Loops but i am new to VBA and cannot get the program to run. Looping this would help me becasue I currently have this same code altered 85 differnt times to fill 85 different columns. For example, the next set is
With Sheet1.Range("Y6")
.Value = 1
.AutoFill .Resize(Y6 + C, 1), xlFillSeries
End With
Is there a more simple way this can be accomplished?
An alternate approach using Offset to fill every third column, starting in V6.
Sub MyNumbering()
Dim c As Long, i As Long
c = 100
For i = 0 To 84
With Sheet1.Range("V6").Offset(, i * 3)
.Value = 1
.AutoFill .Resize(c), xlFillSeries
End With
Next i
End Sub
This will iterate every 3rd column starting with column V and post 85 columns of row numbers starting in row 6 and ending at C + 6
Sub mynum()
Dim c As Long: c = 100
Dim j As Long
For j = 22 To 22 + 85 * 3 Step 3
With Sheet1.Range(Sheet1.Cells(6, j), Sheet1.Cells(c + 6, j))
.Formula = "=ROW(1:1)"
.Value = .Value
End With
Next j
End Sub

Inserting blank rows after specific rows - VBA

This is a code I came up with until now
Sub RunMe()
Dim x As Integer
Dim sv As Integer
x = 11
sv = Range("MyTable").Rows.Count
Do
Rows(x).Resize(sv).Insert
x = x + sv
Loop Until IsEmpty(Cells(x, "A"))
End Sub
So basically this is supposed to insert blank cells in row 11. But the problem is it just adds blank cells at the end of the table instead after row 11. Is there any way to fix this? I am a pure beginner at VBA, this is my first time experimenting with it. Any help will be appreciated.
This could be achieved by:
LastRow = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
Sheet2.Range("A1:K" & LastRow).Copy 'amend to include the number of columns, also change 1 to 2 if using header and don't want to include them
Sheet1.Rows("11:11").Insert Shift:=xlDown
This will not create empty rows, but instead it will insert all the rows with data into Sheet1 Row 11.
So count the number of rows on Sheet2, then select the range to be copied, and finally inserting into row 11 of Sheet1.
UPDATED:
Sheet2.Range("MyTable").Copy
Sheet1.Rows("11:11").Insert Shift:=xlDown
Try this, for me it works quite ok:
Sub RunMe()
Dim x As Integer
Dim sv As Integer
x = 11
sv = Range("MyTable").Rows.Count + 1
Rows(x).Resize(sv).Insert
End Sub
If the table is with 23 rows, it inserts 23 empty rows after the 10. row.

VBA Excel: Insert a new column every nth column filled with a formula which refrences the immediate column to the left

I would like to insert a new column every other column about 260 times, then I need to fill the new columns with a formula which references the immediate column to the left. Here is what I have to insert a new column:
Sub insert_column_every_other()
For colx = 2 To 266 Step 2
Columns(colx).Insert Shift:=xlToRight
Next
End Sub
But I am stuck on the formula, it merely copies the formula as is.. I need the C3 value to change and reference the immediate column to the left (other parts might not be 100% I am new to VBA so all corrections are appreciated):
Sub Repeat()
For ColNum = 3 To 2000 Step 2
Range(Cells(2, ColNum), Cells(21, ColNum)).FormulaR1C1 ="=AVERAGE(OFFSET(C3,1,0,2,1))"
Next ColNum
End Sub
I think the next code should do what you want
Sub insert_column_and_Formula()
Dim colx As Long
Dim H As Worksheet
Set H = H3 'Replace H3 with the sheet that contains your data
For colx = 2 To 266 Step 2
'Insert the Column'
Call H.Columns(colx).Insert(Shift:=xlToRight)
'Put the formula in the new Column'
H.Range(H.Cells(2, colx), H.Cells(21, colx)).FormulaR1C1 = "=AVERAGE(OFFSET(RC[-1],1,0,2,1))"
Next colx
End Sub
Hope this help you, any question let me know please

vba that will select not in common text

I Know how to build a loop macro to read all of the rows in a certain column, but what I do not know how to do is write it in such a way that it will select the data not in common and that may not sound correct so here is an excample:
column A
1
1
1
1
2
2
2
2
3
3
3
What I want is for the loop to look at column A but only the first row of the new data so it would look like this
Column A
1
2
3
Thank you,
Give this a try:
Sub Macro1()
Dim A As Range
Set A = Range(Range("A1"), Range("A" & Cells(Rows.Count, "A").End(xlUp).Row))
A.RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
EDIT#1:
This will first copy to another sheet and then remove duplicates in the copy:
Sub Macro1()
Dim A As Range, B As Range
Set A = Range(Range("A1"), Range("A" & Cells(Rows.Count, "A").End(xlUp).Row))
Set B = Sheets("Sheet2").Range("A1:A" & A.Rows.Count)
A.Copy B
B.RemoveDuplicates Columns:=1, Header:=xlNo
End Sub

Comparing the cell values and printing the count in Excel using a formula or function?

I need a formula or function which is going to fulfill my below mentioned need. I have a excel data of around 11000 rows and data looks somewhat like in Column A:
Now in column B i want the result to be printed like it mentioned below: which literally means it should count the values present in column A and print it in the column B, I don't need to repeat count:
Column A Column B
PC-101 1
PC-101 1
PC-102 2
PC-102 2
PC-103 3
PC-104 4
PC-106 5
PC-107 6
PC-104 4
PC-106 5
PC-106 5
I tried with the "count" series formulas but the result was null.
Even i wrote the macro as given below( which i got from stackoverflow) but even it is printing the repeating count:
Sub CountOccurence()
' Reference: Microsoft Scripting Runtime
Application.ScreenUpdating = False
Set oDict = New Dictionary
Dim wS As Worksheet
Dim r As Integer, rLast As Integer
Set wS = Sheet1
rLast = wS.Cells(1, 1).CurrentRegion.Rows.Count
For r = 3 To rLast Step 1
If Not (oDict.Exists(wS.Cells(r, 1).Value)) Then
oDict.Add wS.Cells(r, 1).Value, 1
Else
oDict.Item(wS.Cells(r, 1).Value) = oDict.Item(wS.Cells(r, 1).Value) + 1
End If
wS.Cells(r, 2).Value = oDict.Item(wS.Cells(r, 1).Value)
Next r
Set oDict = Nothing
Application.ScreenUpdating = True
End Sub
Can anyone help me regarding this? Thanks in advance.
I tried with the "count" series formulas but the result was null.
A simple Excel formula can do this.
Put 1 in Cell B1 and then put this formula in cell B2 and pull it down.
=IF(COUNTIF($A$1:$A2,A2)>1,VLOOKUP(A2,A:B,2,0),B1+1)
Assuming that your data in column a is sorted, you can simply place 1 in B2 this formula in B3 and copy it down:
=IF(A2<>A3,B2+1,B2)
:-)