Select the dates cell only from column in Excel - excel

I am working on excel sheet, in a column having multiple dates and other numbers. I trying to select the date only and copy it to next column. I tried to use Find and replace option but its not working, i used the script of KUTOOLS, but its selecting the range of values. So value falls in the same range of two different values, so could not differentiate. How to select the cells only date in Excel.
Sub FindReplace()
'Update 20150423
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
If Rng.Value > 500 Then
Rng.Value = 0
End If
Next
End Sub
Below is the excel sheet

You could try:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long
With ThisWorkbook.Worksheets("Sheet1") '<- Refer to the sheet you want
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<- Find the last row of column A
For i = 2 To LastRow '<- Loop from row 2 to last row
If IsDate(.Range("A" & i).Value) Then '<- Check if column A row i is date
'Code Here
End If
Next i
End With
End Sub

You can use the IsDate function on cell.Value (not Value2) to filter if the value is in DATE format.
Dim rng As Range
Set rng = Worksheets(1).Range("A2", "A5")
Dim cell As Range
For Each cell In rng.Cells
With cell
If IsDate(cell.Value) Then
Debug.Print cell.Value
End If
End With
Next cell

Related

Bulk find and replace same values of in cells of a column

I am trying to create an Excel VBA function to search values of column A and find same cells in column H, and replace these cells in B2:F6 with values of J2:N4.
My Input File:
Desired Output:
I have tried the following VBA code but it doesn't work. it finds and replace the values of column Replace1 and ignores Replace 2,3,... .
Sub MultiFindNReplace()
'Update 20140722
Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Original Range ", xTitleId, InputRng.Address, Type:=8)
Set ReplaceRng = Application.InputBox("Replace Range :", xTitleId, Type:=8)
Application.ScreenUpdating = False
For Each Rng In ReplaceRng.Columns(1).Cells
InputRng.Replace what:=Rng.Value, replacement:=Rng.Offset(0, 1).Value
Next
Application.ScreenUpdating = True
End Sub
Looks like both datasets got same headers so you can benefit from that. If the headers are always the same and same sorting, just copy whole row:
Sub test()
'if headers of both datasets are always the same and sorted the same way, just copy whole row
Dim rngDestiny As Range
Dim rngSource As Range
Dim rngFind As Range
Dim rng As Range
Dim i As Long
Dim RowN As Long
Dim LR As Long
Set rngSource = Range("I2:M4")
Set rngFind = Range("H2:H4")
Set rngDestiny = Range("B2:F6")
LR = Range("A" & Rows.Count).End(xlUp).Row 'last non-blank cell in column f-name
For i = 2 To LR Step 1
With Application.WorksheetFunction
'check if the value of f-name exists in column FIND
If .CountIf(rngFind, Range("A" & i).Value) > 0 Then
'there is a match, get row number and copy
RowN = .Match(Range("A" & i).Value, rngFind, 0)
rngSource.Rows(RowN).Copy rngDestiny.Rows(i - 1) 'minus 1 because our first row of data starts with i=2!!!
End If
End With
Next i
Set rngSource = Nothing
Set rngFind = Nothing
Set rngDestiny = Nothing
End Sub

MS EXCEL | Find and Replace | Multiple Values at once | All worksheets at once

I want to replace multiple values in an Excel workbook with multiple worksheets all at once. See screenshots:
Multiple Find and Replace Table
Sheet for replacement
Sheet for replacement
Sheet for replacement
I also want the color in background of the cell to be copied from "Multiple Find and Replace Table" for replacement.
I found this VBA code,
Sub MultiFindNReplace()
'Updateby Extendoffice
Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Original Range ", xTitleId, InputRng.Address, Type:=8)
Set ReplaceRng = Application.InputBox("Replace Range :", xTitleId, Type:=8)
Application.ScreenUpdating = False
For Each Rng In ReplaceRng.Columns(1).Cells
InputRng.Replace what:=Rng.Value, replacement:=Rng.Offset(0, 1).Value
Next
Application.ScreenUpdating = True
End Sub
But it only works for selection on one worksheet. Is there a way to do it for multiple selected worksheets or the whole workbook?
Please, test the next code. You should adapt the active sheet, or using the test workbook let "Sheet1" to be the one where the column B:B values to be copied from:
Sub testReplaceValuesAllSheets()
Dim ash As Worksheet, ws As Worksheet, lastR As Long, rngC As Range
Set ash = ActiveWorkbook.Sheets("Sheet1") 'use here the sheet with the column to replace the others
lastR = ash.Range("B" & ash.Rows.count).End(xlUp).row
Set rngC = ash.Range("B2:B" & lastR): rngC.Copy
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Sheets
ws.Range("A2").PasteSpecial xlPasteAll
Next
Application.ScreenUpdating = True
MsgBox "Ready..."
End Sub
The above code will also replace the column A:A cells value. If you need to be excepted, I can adapt the code to do it.
Please, test it and send some feedback.

How to select cells between two values in column and draw the chart from selected elements

I have a problem. I need to write the macro which select the values in column E.
Values of selected items should be between values in cell T2 and U2.
After selection, macro should draw the chart.
I tried 3 ways:
First Approach:
Sub wykres1()
Dim rng As Range
Dim cell As Range
Set rng = Range("E1", Range("E65536").End(xlUp))
For Each cell In rng
If cell.Value > "T2" and cell.value < "U2" Then Cell.Select
With Selection
ActiveSheet.Shapes.AddChart2
End With
Next cell
End Sub
Wykres1 Doesn't work, because the line with if is highlighted on red.
Second Approach:
Sub wykres2()
Dim rng As Range
Dim cell As Range
Set rng = Range("E1", Range("E65536").End(xlUp))
For Each cell In rng
If cell.Value > ActiveSheet.Cell(2,20).Value and cell.value < ActiveSheet.Cell(2,21).Value Then Cell.Select
With Selection
ActiveSheet.Shapes.AddChart2
End With
Next cell
End Sub
Wykres2 Doesn't work, because the line with if is highlighted on red.
Third Approach:
Sub wykres3()
Dim rng As Range
Dim cell As Range
Set rng = Range("E1", Range("E65536").End(xlUp))
For Each cell In rng
If cell.value > -35 And cell.value < -32 Then cell.Select
With Selection
ActiveSheet.Shapes.AddChart2
End With
Next cell
End Sub
Wykres3 freeze after run. When I remove the part with draw chart, the
macro select one cell not the range with selected values. And here I
put the values in macro (-35) (-32) - but I'm interested in possibility
to put values from cells (T2) (U2).
As I mentioned - I need to create macro which select the cells in column E with values between values in cells T2 and U2. After selection macro must draw the chart.
Thank You for Your help.
Try this (Untested). avoid the use of .Select. Work with objects. You may want to see How to avoid using Select in Excel VBA
Sub wykres1()
Dim rng As Range, cell As Range
Dim lRow As Long, i As Long
Dim ws As Worksheet
'~~> Change as applicable
Set ws = Sheet1
With ws
'~~> Find last row in Col E
lRow = .Range("E" & .Rows.Count).End(xlUp).Row
'~~> Loop though the range
For i = 1 To lRow
If .Range("E" & i).Value > .Range("T2").Value And _
.Range("E" & i).Value < .Range("U2").Value Then
With .Range("E" & i)
'
'~~> Do Something
'
End With
End If
Next i
End With
End Sub
As I mentioned - I need to create macro which select the cells in column E with values between values in cells T2 and U2. After selection macro must draw the chart.
You can store each range found above in one range object and then use that. See this example
Sub wykres1()
Dim rng As Range, cell As Range
Dim lRow As Long, i As Long
Dim ws As Worksheet
Dim Obj As ChartObject
'~~> Change as applicable
Set ws = Sheet1
With ws
'~~> Find last row
lRow = .Range("E" & .Rows.Count).End(xlUp).Row
'~~> Liip though the range
For i = 1 To lRow
If .Range("E" & i).Value > .Range("T2").Value And _
.Range("E" & i).Value < .Range("U2").Value Then
'~~> Store the cell in a range object
If rng Is Nothing Then
Set rng = .Range("E" & i)
Else
Set rng = Union(rng, .Range("E" & i))
End If
End If
Next i
'~~> Once you have the range, create a chart and assign range
If Not rng Is Nothing Then
With .ChartObjects.Add(Left:=100, Width:=375, Top:=75, Height:=225)
.Chart.SetSourceData Source:=rng
.Chart.ChartType = xlColumnClustered
End With
End If
End With
End Sub

Excel VBA - Copying a cell within an array and pasting to next empty cell

New to VBA and trying to write a macro that will copy a reference number (column A) of an entry if column Y contains certain text (CHK). I have been able to set up an array that will check if a cell contains the value CHK and copies the reference number if it does (with this then repeating for each cell).
What i am struggling with is pasting the values for each cell into the next empty cell in row A of another workbook. I have managed to copy the value into the next empty cell but I am unsure how to then move one cell down for the next run through of the array. Whereas, at the moment the value in the cell is overridden each time the array runs
My current code is shown below:
Sub Copy_detailed_WithNum_V2()
Application.ScreenUpdating = True
Dim ws1 As Worksheet, ws2 As Worksheet
Dim SrchRng As Range, cel As Range
Set ws1 = Sheets("Detailed Register-All")
Set ws2 = Sheets("VIPP Register")
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
'Activate Detailed Reigster sheet
ws1.Activate
Set SrchRng = Range("Y:Y")
For Each cel In SrchRng
'Check if the cell contains CHK text
If InStr(1, cel.Text, "CHK") Then
'Copy rerference number if entry has CHK value
cel.Offset(0, -24).Copy
'Activate VIPP Register sheet
ws2.Activate
'Paste in the next empty cell in Coulmn A
Cells(lastRow + 1, 1).PasteSpecial xlPasteValues
End If
'Check next cell
Next cel
End Sub
The issue is likely due to your instances of Range and Cells not being qualified with a worksheet. Also, notice that you do not need to Activate a worksheet in order to modify it.
Instead of copying values, you can just set the value of 2 ranges equal to each other which is what I have done here.
Laslty, your search range is currently set to Y:Y which is the entire column (a little over 1 million cells to check). You need to minimize this to a minimal/necessary range. I have this set to start in Y2 (assuming you have a header) and scan down to the last used cell in column Y
Sub Copy_detailed_WithNum_V2()
Dim ws1 As Worksheet: Set ws1 = Sheets("Detailed Register-All")
Dim ws2 As Worksheet: Set ws2 = Sheets("VIPP Register")
Dim SrchRng As Range, cel As Range, lastRow As Long
Set SrchRng = ws1.Range("Y2:Y" & ws1.Range("Y" & ws1.Rows.Count).End(xlUp).Row)
Application.ScreenUpdating = False
For Each cel In SrchRng
If InStr(1, cel.Text, "CHK") Then
lastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Offset(1).Row
ws2.Cells(lastRow, 1).Value = cel.Offset(0, -24).Value
End If
Next cel
Application.ScreenUpdating = True
End Sub

copy cells in a range to a column

i have MyRange = A1:C3
current values in the range are as below:
A1=a, B1=d, C1=f
A2=b, B2=e, C2=""
A3=c, B3="", C3=""
the blank cells in the range can vary.
how can i copy (using vba) non-blank values from MyRange and paste them all together let's say to column AA?
Eg:
AA1=a
AA2=b
AA3=c
AA4=d
AA5=e
AA6=f
Thanks again guys :-)
Paul
Iterate through all cells in MyRange, if cell is not "" copy value to next target cell
Sub test()
Dim MyRange as Range
Dim TargetCell as Range
Dim rw as Range
Dime cl as Range
Set MyRange = ActiveWorkbook.Names("MyRange").RefersToRange
Set TargetCell = Range("AA1")
For each rw in MyRange.Columns
For each cl in rw.Cells
If cl.value <> "" then
TargetCell = cl.value
Set TargetCell = TargetCell.Offset(1,0)
End If
Next
Next
End Sub

Resources