I have put together a calendar (see image) in excel and one of the features I am trying to work out is how to extract the tasks assigned that day and copy these tasks onto another worksheet.
I have updated the code to the following
Sub OtherTask()
Dim DRng As Range
ActiveSheet.Range("g2:ah2").Find(Date).Select
ActiveCell.Resize(5).Offset(4).Select
Selection.AutoFilter Field:=1, Criteria1:="1", Operator:=xlFilterValues
'Selection.Copy
'ActiveSheet.Range("r12").PasteSpecial xlPasteValues
'Application.CutCopyMode = False
If ActiveSheet.AutoFilterMode = "True" Then
ActiveSheet.AutoFilterMode = "False"
End If
End Sub
I have declared DRng as the Range but I cannot figure out how to set it so that DRng represents the following
ActiveSheet.Range("g2:ah2").Find(Date).Select
ActiveCell.Resize(5).Offset(4).Select
Selection.AutoFilter Field:=1, Criteria1:="1", Operator:=xlFilterValues
The end goal is to have the excel sheet recognise the that day's date - accomplished with the following
ActiveSheet.Range("g2:ah2").Find(Date).Select
Once the date is established look down the column and filter any cells that have the # "1" in the cell - accomplished with the following
ActiveCell.Resize(5).Offset(4).Select
Selection.AutoFilter Field:=1, Criteria1:="1", Operator:=xlFilterValues
Once this has occurred I would ideally like excel to then go back towards Column B (I've been trying this will off set and a negative number - no luck) and then copy what is in Column B corresponding to the Row where a "1" is present. Looking at the image an example would be on February 19th I would like excel to be able to find activity 1A (which is in Cell B7) and copy this cell.
My thinking was that once I was able to locate the day and the cells with a "1" in them I would be able to Set this as the Range DRng and then use DRng to offset over to column B and grab the appropriate cells. Perhaps (more than likely) I am thinking about this the wrong way as the goal cannot be accomplished with what i have here. I've been toying with an if statement between With/End With but no luck. Any ideas/direction is greatly appreciated. Thanks
Hmm I'm not sure if I interpret your question correctly since it's a little vague. Do you want to go down the column and grab the corresponding Activity and put them in a new sheet? If so, try this
Sub TodaysDate()
Dim DRng As Range
Dim TASKng As Range
Dim newSh As Worksheet
'set the column to loop
col = Range("g2:ah2").Find(Date).column
'create new worksheet
Set newSH = thisworkbook.worksheets.add
tarRow = 1 'set which row to start writing data
For i = 6 to 36 'set which rows to loop
If activesheet.cells(i,col).value = 1 then
newSH.cells(tarRow,1) = activesheet.cells(i,1)
tarRow = tarRow +1
End if
Next i
Related
I'm using excel vba to delete filtered rows. The code is working just when I specified the range to be on A1 and put my table headers on A1. But, my table headers on B9 so I need to put it on the range but that error occurs. I didn't know why its working for Range("A1") and it didn't work for Range("B9"). In addition when I put A1 as my range to my table it deleted all the rows not just the filtered rows.
Sub Delete_CD_Blanks()
Dim Rng As Range
Dim Rng_Del As Range
Set Rng = Range("B9").CurrentRegion
If Sheets("tt").AutoFilterMode = True Then
Sheets("tt").AutoFilter.ShowAllData
End If
' Rng.AutoFilter field:=4, Criteria1:=” = ”
Rng.AutoFilter field:=6, Criteria1:="??? ?????"
Rng.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
Sheets("tt").AutoFilterMode = False
End Sub
You did not ask my clarification questions and I (only) suppose that your problem stays in the fact that there are some cells above the 9th row, which make part from the CurrentRegion. If I am right, please add a code line, able to create a range starting from 9th row (inclusive):
'your existing code
Set rng = Range("B9").CurrentRegion 'existing in your code
Set rng = Intersect(rng, ActiveSheet.rows("9:" & rng.rows.count)) 'it creates a range slice, starting from 9th row and ending to the `CurrentRegion` last row
'your existing code
I need to work on a range of cells that changes every day. I have decided that the best way to handle this might be to use an InputBox to get the range to work on. The macro massages the data and places it 1 column to the right of the range from the InputBox, but the data is not in adjacent cells (if that makes a difference.)
I would like to select the cells 1 column to the right of the selected range where the new data is located (this is the preferred solution) and format the new data. Or if I can't select the range, I could just select the entire column and then change the format of the entire column.
I can't figure out how to extract the range info to do the necessary math on it and then use it to change the format of the newly created data.
I have included a simplified sample of the problem area of the code.
I would appreciate your help with this.
Sub InputBox_Range_Test()
Dim rng As Range
Set rng = Application.InputBox(Prompt:=PromptString, Type:=8)
Debug.Print rng.Address
'
'*** Needs to select the range that is
'*** 1 column to the right of the input range
'
Columns(rng).Select
Selection.NumberFormat = "0.00%"
Range("I4").Select
End Sub
I finally figured it out. I was thinking about it the wrong way. I was trying to make it harder that it needed to be. I just had to use the offset property.
Here's what I came up with:
Private Sub CommandButton2_Click()
Dim rngTarget As Range
Set rngTarget = Application.InputBox(strPrompt, "Select the Range to work with", Type:=8)
Range(rngTarget.Address).Offset(, 1).Select
Selection.NumberFormat = "0.00%"
Range("P2").Select
End Sub
I am attempting to delete a specific table row based on values in two columns. I attempted to apply filters to the table columns to narrow my criteria, but once I click delete, the ENTIRE ROW is deleted causing values outside of the table to be deleted. Also, the macro recorder isn't as dynamic as I'd like it to be, since it ONLY selects the cell I clicked while recording.
Sub Macro2()
'
' Macro2 Macro
'
'
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"Apple" \\Narrowing criteria in Column 1 of the table
Range("A4").Select \\This only applies to a specific cell, and the value can shift
Selection.EntireRow.Delete \\This will delete the entire sheet row, I'd like for only the table row to be deleted
Range("A5").Select
Selection.EntireRow.Delete
Selection.EntireRow.Delete
End Sub
Is there a way to find the desired string in a column and delete only the rows in the table once the criteria is met? I attempted to only delete the ListObject.ListRows, but it only references the row I've selected, and not the one based off criteria.
You could use .DataBodyRange and .SpecialCells(xlCellTypeVisible) to set a range variable equal to the filtered ranges, then unfilter and delete:
Dim dRng As Range
With ActiveSheet.ListObjects("Table1")
.Range.AutoFilter Field:=1, Criteria1:="Apple"
If WorksheetFunction.Subtotal(2, .DataBodyRange) > 0 Then
Set dRng = .DataBodyRange.SpecialCells(xlCellTypeVisible)
.Range.AutoFilter
dRng.Delete xlUp
End If
End With
You will have to indicate which cells/range you want to delete. You can find the relevant row by using the find function. Since your table is static I would propose the following macro.
A for loop checking each row is also possible, but not so efficient for a very large table. It can be useful to prepare your dataset by adding a flag to column c (e.g. a 1 if to be deleted).
EDIT suggestion by Tate also looks pretty clean
Sub tabledelete()
Dim ws As Worksheet
Dim rangecheck As Range
Dim rcheck As Integer
Set ws = Sheets("Sheet1") 'fill in name of relevant sheet
Set rangecheck = Range("A1") ' dummy to get the do function started
Do While Not rangecheck Is Nothing
With ws
With .Range("C2:C30") ' fill in relevant range of table
Set rangecheck = .Find(what:=1, LookAt:=xlWhole)
End With
If Not rangecheck Is Nothing Then 'only do something if a 1 is found
rcheck = rangecheck.Row
.Range(.Cells(rcheck, 1), .Cells(rcheck, 3)).Delete Shift:=xlUp 'delete 3 columns in row found
End If
End With
Loop
End Sub
I am working with a spreadsheet to produce a report. The data will vary depending on the department for which I am reporting. I need to select the records in column A that are the result of a filter and paste them in column B, once pasted I need to clear the cells in column A then remove the filter. I have been able to accommodate the variable criteria for the filter; however, my problem is with selecting the variable range. The data to be selected will never start in cell A2, any cell after that is fair game. How do I select a range that varies? How do I have the User interact with the macro to select the cells?
This works for me to filter by all my variables.
Cells.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$F$65000").AutoFilter Field:=1, Criteria1:=Array( _
"1000", "1001", "1005", "ZBIL", "1002", "1003", "1004", "1006", "1007", "1008", "1009", "AOMS", "ASPS", "NATL", "ZCON", "ZREP"), Operator:=xlFilterValues
On Error Resume Next
I am using the following to select the range, the first line works to select the data to be copied (although it still selects the cell A1 and I would prefer not to have that cell selected). The second line returns an error "Object doesn't support this property or method. I have tried range select ToRight however I end up with the entire sheet selected.
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Selection(Selection, Selection.xlToRight).Select
Selection.FillRight
I appreciate any assistance that is available.
It might not be the most efficient solution, but you can loop through all the cells/rows and check if the filter hid them:
Dim lastrow As Integer
Dim i As Integer
With ActiveSheet
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To lastrow
If .Rows(i).Hidden = False Then
.Cells(i, 2).Value = .Cells(i, 1).Value
.Cells(i, 1).Value = ""
End If
Next i
End With
I'm having the same kind of question as in this link -
Get AutoFilter sort criteria and apply on second sheet
I've gone thru the link but not able to get the required output.
I've the filtered criteria in Sheet1 (which we can change as required) on one of the column values (eg: col 10) and now based on what ever the data in column 10 which are shown based on the filter criteria, I want to filter on sheet2 with the data in sheet 1.
I have seen that many of them using with static values in ARRAY as shown but how can I autofilter dynamically changing values in the sheet1 and filtering in Sheet2. Please advise
.AutoFilter Field:=10, Criteria1:=Array("value1", "value2"), Operator:=xlFilterValues
What if you just define the array in VBA?
Dim CritArray(2) as String
CritArray(0) = Cells(1,1).Value
CritArray(1) = Cells(2,1).Value
Then just editing your line of code:
.AutoFilter Field:=10, Criteria1:=Array(CritArray(0),CritArray(1)), Operator:=xlFilterValues
I dont know how many criteria you have (or their location), but you can add/edit as such. I based this off the fact you only have 2 criteria, but it can be enlarged of course.
I think you want something like this:
Sub tgr()
Dim wsData As Worksheet
Dim wsCriteria As Worksheet
Dim arrCriteria As Variant
Set wsData = Sheets("Sheet2")
Set wsCriteria = Sheets("Sheet1")
arrCriteria = Application.Transpose(wsCriteria.Range("J4", wsCriteria.Range("J4").End(xlDown)).Value)
With wsData.UsedRange
.AutoFilter 10, arrCriteria, xlFilterValues
End With
Set wsData = Nothing
Set wsCriteria = Nothing
If IsArray(arrCriteria) Then Erase arrCriteria
End Sub