vba code problem

Status
Not open for further replies.

Soha

Banned User
Joined
Aug 22, 2022
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
Where is the problem in the code that the columns m, n and o are emptied despite specifying the emptying range

sh.Range("C10:L1000").ClearContents

Sub derc()
Dim arr As Variant
Dim temp As Variant
Dim cr As Variant
Dim lr As Long
Dim i As Long
Dim j As Long
Dim C As Long
Dim WS As Worksheet
Dim sh As Worksheet
Dim myArray, targt, targt2

Set Main = Sheets("sh1")
Set sh = Sheets("sh2")

targt = sh.Range("M5").Value & "*"
targt2 = sh.Range("M6").Value & "*"
'targt = "ذك*"
'targt2 = "نا*"


'= = = = = = = = = = = =

sh.Range("C10:L1000").ClearContents

lr = Main.Cells(Rows.Count, 4).End(xlUp).Row


arr = Main.Range("A10:R" & lr).Value

ReDim temp(1 To UBound(arr, 1), 1 To UBound(arr, 2))

cr = Array(2, 4, 5, 7, 9, 10, 11, 12, 15)
j = 1

For i = LBound(arr, 1) To UBound(arr, 1)

'If arr(i, Like targt & "*" _
And arr(i, 89) Like targt2 & "*" Then


'==================
temp(j, 1) = j
For C = LBound(cr) To UBound(cr)
temp(j, C + 2) = arr(i, cr(C))
Next C
j = j + 1
'==================
'==================
Next i
With sh

.Range("C10").Resize(j - 1, UBound(temp, 2)).Value = temp

End With
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I doubt this line:
VBA Code:
sh.Range("C10:L1000").ClearContents
is causing your problem.

It is more likely this line:
VBA Code:
.Range("C10").Resize(j - 1, UBound(temp, 2)).Value = temp
That is overwriting values in columns m, n, and o.


(Tip: when posting code, please try to use 'code tags' to format the code as I have done above
as it makes the code easier to read.)
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,599
Messages
6,120,453
Members
448,967
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top