I have a bunch of .csv data reports i need to clean up in Excel.
I have been trying to write a macro that would select everything, then deselect a few specific rows and columns from the selection.
I don't need the code to go any further than that as i have another macro that works for deleting everything.
The nice thing is that the data im removing is consistent so i can hard code what needs to go bye bye.
Ive got some C# exposure but not VBA and particularly to MS Excel. I found some code I have been trying to adapt but am not getting it to do anything or just error out. At this point I was thinking to get it to do one line, then figure out how to remove the others.
Can I get some pointers on how to Select everything minus A row or column?
Sub Macro1()
'
' Macro1 Macro
' report format
'
Dim rng As Range
Dim InputRng As Range
Dim DeleteRng As Range
Dim OutRng As Range
' xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
' Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set DeleteRng = Application.Rows(7)
For Each rng In InputRng
If Application.Intersect(rng, DeleteRng) Is Nothing Then
If OutRng Is Nothing Then
Set OutRng = rng
Else
Set OutRng = Application.Union(OutRng, rng)
End If
End If
Next
OutRng.Select
'
End Sub
So i feel silly as I completely overlooked that Excel can attempt to auto record macros. I gave it a whirl and it did what I needed to do.
here is the auto generated code to produce the end result I needed. It only highlights the desired cells. From there I can use keyboard shortcuts to format the sheet.
I absolutely don't understand what it did but here it is.
Sub work()
'
' work Macro
'
'
Range("M3:XFD1048576,F3:J1048576,C3:C1048576").Select
Range("C3").Activate
End Sub
Sub Macro()
Dim wb As Workbook
Set wb = ThisWorkbook
'Consider column(2) as B column
wb.Worksheets("Sheet1").Columns(2).EntireColumn.Delete
Related
I am using this code to set a range of filtered table to visible cells only.
Set rngMyRange = Selection.SpecialCells(xlCellTypeVisible)
However, there is a strange bug if only one cell is selected, that is, the code selects all used range in the filtered table.
If the selection is greater than one cell, then the code works as expected. Is there a way around this?
Usually using "Selection" is not a good practice; I am guessing you just need to get the range of the visible cells for that you can easily use this:
Sub GetVisibleRangeOnly()
Dim tbl As ListObject
Dim rng As Range
'change the name of the table and worksheet as you need
Set tbl = Worksheets("Sheet1").ListObjects("Table1")
'Note: if there is no visible cell after filtraton rng IS NOTHING will be TRUE
Set rng = tbl.DataBodyRange.SpecialCells(xlCellTypeVisible)
End Sub
I make a lot of data entry mistakes and I am trying to come up with a way to verify my input. I copy values from cells to another, I figure it would be best if I linked to the cell directly and then had those cells automatically colored.
Here is my proposal:
Selection.Copy
Selection.Interior.ColorIndex = 37
Set rng = Application.InputBox("Cell reference", Type:=8)
Now I cannot figure out a way to paste the links to the input cell reference. It seems like by selecting a cell with the input box, the selection is lost.
So, you want to select a cell and change its contents based on another cell contents, right? You are creating a reference to the source cell by using the set statement. Now, you just have to use the .address property of your range to get a string value that represents the range reference in the language of the macro (See help for this property).
Option Explicit
Sub CopyingCellContents()
Dim rng As Range
Selection.Copy
Selection.Interior.ColorIndex = 37
Set rng = Application.InputBox("Cell reference", Type:=8)
Selection.Value = activesheet.range(rng.Address)
End Sub
A tip: ALWAYS set the Require Variable Declaration in your code.
Considering your further explanation and your own code, I tried to update yours.
Sub xxx
Dim rng As Range
Dim inp As Range
Dim Sh as worksheet 'Worksheet where your range is.
set Sh= workbooks("Name Of The Workbook").worksheets("Name Of The Worksheet")
Set inp = Selection
inp.Interior.ColorIndex = 37
Set rng = Application.InputBox("Copy to", Type:=8)
sh.activate
inp.Copy Destination:=rng, Link:=True
End sub
Change "Name Of The Workbook" and "Name Of The Worksheet" by the names of the workbook and and worksheet respectively where the ranges you want to manipulate are. Don't forget to use "".
I have reworked some code. Here it is :
Dim rng As Range
Dim inp As Range
Selection.Interior.ColorIndex = 37
Set inp = Selection
Set rng = Application.InputBox("Copy to", Type:=8)
inp.Copy
rng.Select
ActiveSheet.Paste Link:=True
It seems to work, thanks!
I am new to the world of VB but I would like to copy data from one tabs on a spreadsheet called Ilog and past this into another tab on the same spredshhet on a tab called Journal.
When the data is pasted to the new tab I'd normally filter is so Blanks are ommited so I would like to be able to get the VB code to do this automatically.
Any help would be greatly appreciated
I am not certain what you are asking, but the following code will copy data from a range on sheet llog and paste in journal. Then loop through and delete cells that are blank.
Sub test()
Dim rng As Range
Set rng = Worksheets("llog").Range("A1:A8")
rng.Copy
Set rng = Worksheets("journal").Range("A1:A8")
rng.PasteSpecial
For Each c In Range("A1:A8")
If c.Value = "" Then
c.Delete
End If
Next c
Set rng = Nothing
End Sub
Depending on the complexity of the range being copied, you could also go with:
Sub test()
Dim rng As Range
Set rng = Worksheets("llog").Range("A1:A8")
rng.Copy
Set rng = Worksheets("journal").Range("A1:A8")
rng.PasteSpecial
rng.SpecialCells(xlCellTypeBlanks).Delete
End Sub
which avoids any looping. If you have a relatively complex range, you may want to look in to using the autofilter and then coping over just the visible rows.
Hi all!
I have been trying to make vba code for the following purpose: copy a range of a workbook (screenshot above A1:F2) to a new workbook.
I have managed to achieve this. There is one additional criteria which i would like to add to the vba code. The vba code should only copy those columns where row 2 has a value filled in.
Thus, looking at the example in the screenshot, this would mean that by running the vba code, I would save to a new workbook the ranges A1:A2, C1:C2, E1:E2.
The new worbook would look like the second screenshot
Any help appreciated! Thanks in advance!
A very useful way of ignoring blanks - without looping - is to use SpecialCells. The code below is probably a little lengthier than needed for your question but it is written so that
It can be adapted to other sheets
It will copy non-blanks from row 2 whether they are values and/or formulae
In absence of seeing your code it copies to a new workbook
code
Sub CopyEm()
Dim WB As Workbook
Dim ws As Worksheet
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Set ws = Sheets(1)
On Error Resume Next
Set rng1 = ws.Rows(2).SpecialCells(xlConstants)
Set rng2 = ws.Rows(2).SpecialCells(xlFormulas)
If rng1 Is Nothing Then
Set rng3 = rng2
ElseIf rng2 Is Nothing Then
Set rng3 = rng1
Else
Set rng3 = Union(rng1, rng2)
End If
If rng3 Is Nothing Then Exit Sub
On Error GoTo 0
Set WB = Workbooks.Add
rng3.Offset(-1, 0).Copy WB.Sheets(1).[a1]
rng3.Copy WB.Sheets(1).[a2]
End Sub
What I would like to achieve is that as soon as the macro is run, the user has to select (by hand, from the Sheet and not by typing the range) a range that would then be set to a Range variable.
Excel does have such functionality in many areas, such as the Data selection settings for Charts but I don't know how to access it. Any and all help is appreciated!
Below will force the user to select a Range:
On Error Resume Next
Dim rng As Range
Do
Set rng = Application.InputBox(Prompt:="Select Range", Type:=8)
Loop While rng Is Nothing
If Not rng Is Nothing Then rng.Select
On Error GoTo 0