|
Hi,
I have two sheets. One having warehouse(18 warehouse) and second having shop (50 Shops).
I need to map shops against warehouse within 200 KM.
I have written one code for the same but need to loop it. Code is mentioned below
Code will copy first warehouse from warehouse sheet and paste it against each all 50 shops in shop sheet.
Then one formula in the sheet (already updated in shop sheet) calculate the difference in KM between each shop and first warehouse. Then filtered out shops within 200 km, copy and paste (transpose) against first warehouse in warehouse sheet. Now I want to do same procedure for second warehouse then third till end. Please help
==================================
Sub RADIUS()
Worksheets("SHOP").Activate
AutoFilterMode = False
Range("f2:g4175").Clear
Worksheets("WAREHOUSE").Activate
Range("C2:d2").Select
Selection.Copy
Worksheets("SHOP").Activate
Range("f2:g4175").PasteSpecial _
Operation:=xlPasteSpecialOperationAdd
Application.Calculation = xlCalculationAutomatic
AutoFilterMode = True
Range("A1:h1").AutoFilter Field:=8, Criteria1:="<=200"
Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy _
Worksheets("WAREHOUSE").Activate
Range("e" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
End Sub
|
|
|
|
|
|
Please help me in the same
|
|
|
|
|
|
Nobody knows what you exactly want to do, so it's always needed that you understand - at least to a certain extent - the code and methods someone gives you. It would make a lot of sense to read the hints Richard gave you, instead of trying to make him do your homework.
You can loop through your worksheets - assuming here that the shop worksheets are all named something with "Shop" - like this:
Sub LoopWorksheets()
Dim ws As worksheet
For Each ws In ActiveWorkbook.Worksheets
If UCase(ws.Name) Like UCase("Shop*") Then
RADIUS ws
End If
Next
Application.Calculation = xlCalculationAutomatic
End Sub
The UCASE is needed, because the function is case sensitive (looking for "Shop" would not match your sheet "SHOP")! I would also recommend you to rename your method, because "radius" is a math function and might lead to confusion.
Your function would then consume every worksheet that your loop passes:
Sub RADIUS(ByVal sheet As worksheet)
You can also streamline your function a lot. Instead of
Worksheets("WAREHOUSE").Activate
Range("C2:d2").Select
Selection.Copy
sheet.Activate
Range("f2:g4175").PasteSpecial Operation:=xlPasteSpecialOperationAdd
you could just use one line:
sheet.Range("f2:g4175").Value = Worksheets("WAREHOUSE").Range("C2:d2").Value There's no need to activate every worksheet before doing something on/with it. This is only needed in certain cases, e.g. if you explicitly want to "Select" an object (sheet, range, cell etc.). Usually you just reference the range like "sheet.range(...)". For example
ActiveWorkbook.Worksheets("WAREHOUSE").Range("A1:H1").Interior.ColorIndex = 3 will paint your WAREHOUSE sheet titles red.
Regards
Mick
modified 26-Apr-16 10:31am.
|
|
|
|
|
|
Most likely you need to fix your code.
|
|
|
|
|
Hello,
I have a funny question which arises from the effects I recognize in a sample app.
At a certain point the sample app reads the files of a directory into an array. Since the path to "System32" was anyway pre-set in the sample, I realized the array gets filled with 2560 strings in the array - despite of more than 5000 files in that directory. After changing the path to another Windows directory ("System"), the array again filled with 2560 files only... No error in both cases. Then I changed the path to a different directory (holding approx. 6000 files), and the "GetFiles" array got filled with exactly the number of files existing.
I tried this several times with other directories, and it seems that everything works (i.e. reads and counts) fine as long as the path isn't a system directory. Is this behaviour normal? Or what else might be going on?
Thank you for any insights
Mick
|
|
|
|
|
Sounds like you're running a 32-bit application on a 64-bit OS. The File System Redirector[^] intercepts your request to access C:\Windows\System32 , and redirects it to C:\Windows\SysWOW64 instead.
You can either compile your application as 64-bit; use the special SysNative alias for the System32 directory; or P/Invoke the Wow64DisableWow64FsRedirection[^] and Wow64RevertWow64FsRedirection[^] functions to disable the file system redirector for your application.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you, Richard, for sharing the knowledge as well as probable solutions. It's very likely that the 32/64bit thing is the reason for the effect.
As I wrote before, it's a sample app I examined, so right now I don't need the solutions. But now I'm more prepared for that kind of possible surprises There's still some queston marks, since SysWOW64 has only 2517 (not 2560) file entries on top-level, but it's good enough for having a first clue and some attention around that issue.
Regards
Mick
|
|
|
|
|
Be careful with the overloads:
string[] files = System.IO.Directory.GetFiles(@"C:\windows\system32", "*.*", SearchOption.AllDirectories); looks into sub-directories, while the simple form
string[] files = System.IO.Directory.GetFiles(@"C:\windows\system32"); does not.
I think that is the reason for the differences you experienced.
|
|
|
|
|
Thank you, Bernhard - that's indeed a very good point to look at. Unfortunately, right now I can't look up which overload was used in the sample... most probably I deleted it after having the code run for demonstration of its methods. But I'm going to check your hint later today.
|
|
|
|
|
Now, thanks to the insights of my helpers, I could make it clearer. I only checked the top-level number of files using
Directory.GetFiles(pfad, "*.*", SearchOption.TopDirectoryOnly))
Here's what I found:
• there's no redirection to "SysWOW64" once "System32" is specified as path. I can get the files of both, once set as path.
• the result 2560 was correct, but I could only verify this number after setting the system files (etc) to visible in the folder options!
• i.e. the GetFiles method counts hidden files as well (which it is supposed to do)
So in the end my question arose out of not considering that, in the Windows directory and it's subdirectories, many files are hidden and/or system files. The GetFiles method was completely ok, only my verification wasn't.
Thank you
Mick
|
|
|
|
|
Hi all,
I feel pretty much lost, despite (or because of?) of reading tons of material about delegates already. Maybe someone could help me understand a bit more and give me some hints?
I have
a) an MDI Form which serves as a plugin host (implements IPluginHost from b)
b) a plugin interface (defining "IPlugin" with an event named UIUpdate, "IPluginHost" and a delegate "UIUpdateDelegate")
c) a plugin (referencing IPlugin)
The plugin class holds a custom control, which works on a long lasting task in a separate thread. The custom control is not only supposed to report the progress of this task to it's own UI (which works), but to the MDI forms Interface as well. If ok, I want to see the progress bar counting up in both of the forms.
In a sample I saw that the plugins long lasting tasks method contained
DirectCast(Me.MdiParent, FrmMain).OnUpdateMe("Loading " & i.ToString, i) <----- not working
Me.OnUpdate("Loading " & i.ToString, i) <----- working
to report the progress to both UIs. This doesn't seem to work with the plugin pattern: The type "FrmMain", having an additional "onUpdateMe" Method, is unknown to the compiler, but replacing it with standard "Form" type doesn't work since this doesn't have the "onUpdateMe" Method. Considering it's a plugin, it would anyway make much more sense to learn how to address the MDI parent form without knowing its type, if that's possible.
Before I have to run around in circles for another few days, please help me out of this trap...
Thank you in advance
Mick
|
|
|
|
|
Hi,
I need to delete the folder C:\Windows\System32\Macromed using vbscript but I consistently get access denied no matter how my code is written. I have used an admin prompt to run the script and used psexec to get System rights but get the same access denied result.
•File Explorer - Can't delete manually without responding to request for elevated privileges. I don't want to click the Continue button because that is likely not the configuration for 99.9% of the clients that I will deploy against.
•User CMD - cannot delete folder with RD using command window
•Admin CMD - script cannot delete the folder
•System CMD (psexec) - script cannot delete the folder.
•Trying to shell out to run RD won't work either.
I thought that this could be due to files existing in subfolders but I get access denied when trying to delete just the files to empty the folder. This folder is created when Flash Player is installed but in this case, Flash Player is removed but the folder is left behind. I think it's unlikely that any of the files (4) involved are in use.
Inspecting the security settings shows me that the folder owner is System with full control and Trusted Installer also has full control. System gets an access denied message when it tries to delete the folder even though System is the owner.
How can I change the folder security so it will allow the script to delete the folder? I am using basic code seen in just about any example given for fso.DeleteFolder. I have seen a few forums that recommend using icacls.exe. I haven't tried that one yet but intend to.
Thanks,
Rob
modified 21-Apr-16 16:45pm.
|
|
|
|
|
The account the the code is running under either doesn't have permissions or the files inside the folder are in use by some running instance of Flash or a plugin in currently running browser. Those are the circumstances you get "Access Denied" under.
|
|
|
|
|
Do you want to delete the folder in any possible way or only with VBScript?
I had the problem that I couldn't delete a big bunch of older windows installation files and, with the help of a real 'guru', found a working solution using shell commands - it was the only working solution I found in many months. It was a matter of broken permissions. To fix really broken permissions, the best is to run these two commands one after the other (Admin Shell):
takeown /f "C:\path\to\folder" /r
icacls "C:\path\to\folder" /reset /T
The first one will give you ownership of all the files, however that might not be enough, for example if all the files have the read/write/exec permissions set to "deny". You own the files but still cannot do anything with them. In that case (which I had) run the second command, which will fix the broken permissions.
I don't know if it would work to call these functions from VBScript, maybe it's worth a try. I'm using Win7, but for Win8.1 it should work the same.
Regards
Mick
|
|
|
|
|
Dear Friends
i have no knowladge on networking so i really dont know how to get this work. it seams like group of classes and how can i intigrate it with my forms to try it ? pleas help me out i need it so badly . can any one show me how to get it working ?!
if you could build it in vb and share it with me as a zip file i would be thankful.
there are the offical examples
1. Client : Example
2. Server : Example
Thanks In Advance
|
|
|
|
|
You know those code samples come in both C# AND VB.NET, right?
All you do is click on the VB tab at the top of the code sample.
|
|
|
|
|
Dear Friend
after pasting the code in my form where should i read incoming signals and how to initiate the connection ???
all these thing i dont know how to do them.
|
|
|
|
|
Those examples are written into Console applications, not Windows Forms apps.
You better do it in Console applications first and learn how the code works before you try to adapt the code to work in Windows Forms.
|
|
|
|
|
I have form1 and there is two flags ("Yes" and "No").when i select "Yes" and click on save button.It will open another form2.Once save form2 it will save form1 automatically.But it is giving error "conversion from string to type integer is not valid".please note that when i select "NO" it is not giving any error.please see my below code.
1)code for form1 save
Public Sub frmElogsheet_SAVE()
btneditelog.Enabled = False
btndelelog.Enabled = False
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
' Dim dr As SqDataReader
conn = GetConnect()
conn.Open()
cmd.Connection = conn
Try
Dim code1 As Integer
Dim code2, code4, code5, code6, code7, code13 As String
Dim code8, code9 As Integer
Dim code3, code10, code11, code12 As Date
code1 = txtallocateelog.Text.ToString
code2 = txtvehicleElog.Text.ToString
code3 = txtRequestdateElog.Text.ToString
code4 = txtDestElog.Text.ToString
code5 = txtdeptElog.Text.ToString
code6 = txttravellerElog.Text.ToString
code7 = txtdvr2.Text.ToString
code8 = txtkmreaddept.Text.ToString
code9 = txtkmarr.Text.ToString
code10 = dtpdepttime.Text.ToString
code11 = dtparr.Text.ToString
code12 = dtpovertime.Text.ToString
code13 = Combacci.Text
'g900 = code2 'it will pass value to accident form
cmd.CommandText = "insert into tbElogsheet(Allocate_id,Number_plate,Depart_date,Destination,Department,Traveller,NAME,km_read_dept,km_read_arr,Departure_time,Arrival_time,Over_time,Accident) VALUES (" & code1 & ",'" & code2 & "','" & code3 & "','" & code4 & "','" & code5 & "','" & code6 & "','" & code7 & "'," & code8 & "," & code9 & ",'" & code10 & "','" & code11 & "','" & code12 & "','" & code13 & "')"
cmd.ExecuteNonQuery()
MsgBox("ELOGSHEET Added Sucessfully")
'frmElogsheet_clear() 'clear elogsheet items after saving
'txtkmreaddept.Text = g101
Catch ex As Exception
MsgBox(ex.Message)
End Try
'conn.Close()
End Sub
2)
Private Sub btnsaveElogsheet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
g900 = txtvehicleElog.Text.ToString
G901 = txtRequestdateElog.Text.ToString
G902 = txtdvr2.Text.ToString
Dim t13 As String
t13 = Combacci.Text.ToString
If t13 = ("Yes") Then
Dim frmeAccident As New frmeAccident 'Dim f1 As New frmeAccident()
frmeAccident.Show()
'Dim f1 As New frmeAccident()
Else
frmElogsheet_SAVE()
frmElogsheet_clear() 'clear elogsheet items after saving
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
3)Save for form2
Private Sub btnsaveacc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsaveacc.Click
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
' Dim dr As SqDataReader
conn = GetConnect()
conn.Open()
cmd.Connection = conn
Try
'frmElogsheet.frmElogsheet_SAVE() 'CALL procedure frmElogsheet_SAVE() from frmElogsheet
Dim t2, t3, t4, t5, t6, t7, t8, t9 As String
Dim t1 As Integer
t1 = txtaccident.Text
t2 = txtaccplate.Text
t3 = txtdriver.Text
t4 = dtpeaccdept.Text
t5 = Cmbpolice.Text
t6 = txtlocacc.Text
t7 = txtamnt.Text
t8 = txtdesc.Text
t9 = Combacctype.Text
cmd.CommandText = "insert into tbeAccident values(" & t1 & ",'" & t2 & "','" & t3 & "','" & t4 & "','" & t5 & "','" & t6 & "'," & t7 & ",'" & t8 & "','" & t9 & "')"
cmd.ExecuteNonQuery()
MsgBox("Data Added Sucessfully")
'CODE CHANGES START
'cmd.CommandText = "Select * from tbDept_proj"
'Dim Adpt As New SqlDataAdapter(cmd.CommandText, conn)
'Dim ds As New DataSet()
'Adpt.Fill(ds, "tbDept_proj")
'grdviewdept.DataSource = ds.Tables("tbDept_proj")
'CODE CHANGES END
'frmeAccident_clear()
'close form start
'Dim frmeAccident As New frmeAccident
'frmeAccident.Close()
'Close()
'close form end
frmElogsheet.frmElogsheet_SAVE() 'CALL procedure frmElogsheet_SAVE() from frmElogsheet
Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
please help on this article urgently.
|
|
|
|
|
And you think we are going to look through that unformatted code dump to try and figure out (or guess) where this error occurs?
|
|
|
|
|
Dim code1 As Integer
code1 = txtallocateelog.Text.ToString
You declare code1 as an Integer but then you attempt to assign a String . The same goes for a bunch of other variables there which you have declared as Integer or DateTime . Take a look at Int32.TryParse Method (String, Int32) (System)[^] and DateTime.TryParse Method (String, DateTime) (System)[^] to solve this issue (other primitive types have equivalent .TryParse(..) -methods).
Apart from that: Calling .ToString() on a property of type String is redundant:
code2 = txtvehicleElog.Text.ToString
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
modified 20-Apr-16 8:40am.
|
|
|
|
|
Don't concatenate strings to form your database queries - use Parameterised queries.
For example:
cmd.CommandText =
"insert into tbElogsheet(Allocate_id,Number_plate,Depart_date,Destination,Department,Traveller,NAME,km_read_dept,km_read_arr,Departure_time,Arrival_time,Over_time,Accident) VALUES (@code1,@code2,@code3,@code4,@code5,@code6,@code7,@code8,@code9,@code10,@code11,@code12,@code13)"
cmd.Parameters.AddWithValue("@code1", code1)
cmd.Parameters.AddWithValue("@code2", code2)
cmd.Parameters.AddWithValue("@code3", code3)
cmd.Parameters.AddWithValue("@code4", code4)
cmd.Parameters.AddWithValue("@code5", code5)
cmd.Parameters.AddWithValue("@code6", code6)
cmd.Parameters.AddWithValue("@code7", code7)
cmd.Parameters.AddWithValue("@code8", code8)
cmd.Parameters.AddWithValue("@code9", code9)
cmd.Parameters.AddWithValue("@code10", code10)
cmd.Parameters.AddWithValue("@code11", code11)
cmd.Parameters.AddWithValue("@code12", code12)
cmd.Parameters.AddWithValue("@code12", code13)
cmd.ExecuteNonQuery()
This might well cure your problem.
If it does not then use the Debugger to determine which line is causing the problem and let us know.
|
|
|
|
|